问C# Blazor:倒计时计时器EN
最编程
2024-02-22 20:09:15
...
@page "/"
<h1>Timer</h1>
<p>@counter</p>
<button @onclick="StartTimer">Start</button>
@code {
private static System.Timers.Timer aTimer;
private int counter = 60;
public void StartTimer()
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += CountDownTimer;
aTimer.Enabled = true;
}
public void CountDownTimer(Object source, System.Timers.ElapsedEventArgs e)
{
if (counter > 0)
{
counter -= 1;
}
else
{
aTimer.Enabled = false;
}
}
}