直接上代码

r
@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);// 强制刷新
    }
 
}
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Roc 微信支付

微信支付

Roc 支付宝

支付宝