C# Blazor:倒计时计时器

@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);//强制刷新
    }

}

  

posted @ 2022-06-30 11:18  kismet82  阅读(452)  评论(0编辑  收藏  举报