Use the System.Timers.Timer class to wait until the user has finished typing in the text field. The onkeyup event triggers for each key press and resets the timer until the last timer raises the OnUserFinish event.
[Index.razor]
@page "/"
@using System.Timers;
<input type="text" @bind-value="dataValue" @bind-value:event="oninput" @onkeyup="@OnValueChange" />
<p>@Content</p>
@code {
private string dataValue { get; set; }
private string Content { get; set; }
private Timer timerObj;
protected override void OnInitialized()
{
timerObj = new Timer(1500);
timerObj.Elapsed += OnUserFinish;
timerObj.AutoReset = false;
}
private void OnValueChange(KeyboardEventArgs e)
{
// remove previous one
timerObj.Stop();
// new timer
timerObj.Start();
}
private void OnUserFinish(Object source, ElapsedEventArgs e)
{
InvokeAsync(() =>
{
Content = $"Typed text: {dataValue}";
StateHasChanged();
});
}
}
Share with