我有一个gif文件,每当有人创建一本新书时,我都会运行它,当提交按钮被点击时,选中的bool
变成true
,反过来,一个检查这个布尔值的if
语句运行并显示图像。
但是,我想在显示3秒后删除此gif。我尝试使用StopWatch类,但当我尝试它时不起作用(SO上有人提到您需要一个Timer
用于Blazor应用程序),所以我求助于使用Timer
。
在这种情况下,问题在于应用程序会停止运行。
@using blazorTestApp.Client.Entities
@using System.Timers
@code
{
Timer timer = new Timer();
public Books CreatedBook {get; set;} = new Books();
bool check = false;
public void OnSubmit()
{
check = true;
}
}
@if(check)
{
<img class="Thanks" src="https://gifsec.com/wp-content/uploads/2022/09/thank-you-gif-1.gif"/>
timer.Interval = 3000;
timer.Start();
timer.Elapsed += (Object sender, ElapsedEventArgs e) => check = false; StateHasChanged(); timer.Stop();
}
1条答案
按热度按时间icomxhvb1#
你不需要建立一个计时器,你可以使用内置的任务计时器一次延迟。这里是一个演示页面,显示一个警告框三秒,而不是你的图像。它显示了两种方式来设置延迟。对于简单的短时间等待使用简单。