我需要通过使用每秒的计时器来动态更新 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();
}
}
Run Code Online (Sandbox Code Playgroud) 我已经尝试了下面的代码片段,但它会引发有关类型转换的错误,因为列表包含整数和双精度值的组合。但list.reduce(min)仅返回整数值。您能为此提供一个解决方案吗?
注意——但是我只有用户的List。dynamic
final List<dynamic> y = [12, 12, 23.5, 2, 19, 1, 23, 16, 29.7, 32, 45, 68, 23, 21];
List<num> yValues = y;
num yMin = yValues.reduce(min);
num yMax = yValues.reduce(max);
Run Code Online (Sandbox Code Playgroud)