【发布时间】:2019-09-10 08:42:21
【问题描述】:
我需要使用每秒计时器来动态更新 DOM 值。
我尝试过每秒增加一次计数器值。但是在那个代码中,我只得到了那个 DOM 值的最终值。我需要 DOM 中的每一秒值。
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="StartLiveUpdate">Click me</button>
<button class="btn btn-primary" @onclick="StopLiveUpdate">Stop</button>
@code {
private static System.Timers.Timer syncTimer;
Random random = new Random();
void StartLiveUpdate()
{
syncTimer = new System.Timers.Timer(1000);
syncTimer.Elapsed += IncrementCount;
syncTimer.AutoReset = true;
syncTimer.Enabled = true;
}
void StopLiveUpdate()
{
syncTimer.Enabled = false;
syncTimer.Stop();
}
int currentCount = 0;
void IncrementCount(Object source, System.Timers.ElapsedEventArgs e)
{
currentCount++;
this.StateHasChanged();
}
}
【问题讨论】:
标签: c# asp.net-core blazor