Reputation: 67
is it possible to use Razor syntax to build a countdown timer in Pages (asp .net core 3.1) project. if yes, please share the steps and code snippets. I am trying to use c# only with razor view (@ {} ) only. sometime like this.
@{
string message = "";
Timer timer;
void TickTimer(Object sender)
{
// do something
}
timer = new Timer(new TimerCallback(TickTimer), null, 1000, 1000);
}
Upvotes: 0
Views: 5540
Reputation: 8459
It can be implemented in blazor:
@using System.Threading;
<h1>@Count</h1>
<button @onclick=@StartCountdown>Start Timer</button>
@functions {
private int Count { get; set; } = 10;
void StartCountdown()
{
var timer = new Timer(new TimerCallback(_ =>
{
if (Count > 0)
{
Count--;
InvokeAsync(() =>
{
StateHasChanged();
});
}
}), null, 1000, 1000);
}
}
Upvotes: 9