直接上代码
@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; | |
// 倒计时结束后需要触发的功能,写在这里 | |
} | |
InvokeAsync(StateHasChanged);// 强制刷新 | |
} | |
} |