Example JS code timer
var clock;
var date;
function startTime(element) {
let timeString = new Date().toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, minute: 'numeric', second: 'numeric'});
let dateString = new Date().toLocaleDateString('en-US', { dateStyle: 'full'});
element.innerHTML = dateString + " - " + timeString;
clock = setTimeout(startTime.bind(null, element), 1000);
}
function stopTime() {
clearTimeout(clock);
}
In your wwwroot folder find index.html
<script src='js/yourJS.js'></script>
In your razor component or page
@inject IJSRuntime jsRuntimeService
<div @ref='timeElement'></div>
@code
{
ElementReference timeElement;
protected overried async Task OnInitializedAsync()
{
await jsRuntimeService.InvokeVoidAsync("startTime", timeElement);
await jsRuntimeService.InvokeVoidAsync("Alertfunction");
}
}