You can delay a task in Blazor by using the Task.Delay() method where the time set for the task to be delayed before proceeding to the next.
<h3>Timer: @Timer</h3>
@code {
[Parameter]
public int Timer { get; set; } = 5;
public async void StartTimerAsync()
{
while (Timer > 0) {
Timer--;
StateHasChanged();
await Task.Delay(1000);
}
}
protected override void OnInitialized()
=> StartTimerAsync();
}
Share with