Search code examples
javascriptc#asp.nettimerwebforms

Asp Timer tick event interrupting client side javascript


The context

I am making a web application that monitors services. I have an UpdatePanel for every service, with the status and buttons to start, stop and restart the service. This is all created by Repeaters and is working fine.

Now I have added an asp:Timer as a trigger for all the UpdatePanels and since I want the statuses to update responsively I need the interval to be fairly small.

The issues

I currently have two issues with this. The main issue (in the title) is that when a button is clicked, I have javascript that uses SweetAlert2 to show a warning and waiting for user input. If the timer ticks during this script, it is interrupted. The warning prompt stays up, but clicking "Confirm" does nothing since the script in the background has been interrupted.

The second and more minor issue is that, during the tick, while the UpdatePanels update, the buttons are unresponsive for a split second. This is not a super big issue, but it is annoying to have to click a button twice, if you manage to click during the tick. Since I want to keep the timer interval fairly small this actually happens quite often.

What I have thought about doing

For the first issue, I don't really know what to do. Could I run the timer on another thread? Would that even work like I want it to? Should I just pause the timer while the javascript runs?

For the second issue, I could possibly make a workaround, putting the buttons in their own UpdatePanel or something. The main reason I want the buttons in an UpdatePanel is to change which ones are displayed depending on the status of the service:

// Set button visability and status color
if (service.Status.Equals(ServiceControllerStatus.Running))
{
    statusSpan.Attributes.Add("class", "status-run");
    btnStart.Visible = false;
}
else
{
    statusSpan.Attributes.Add("class", "status-stop");
    btnStop.Visible = false;
    btnRestart.Visible = false;
}

I guess I am mainly looking for the most "correct" way of solving this "refresh issue" in a Web Forms application. But I'll take any "hacky" solution, as long as it gets the job done.


Solution

  • While making a workaround for my second issue, I accidentally solved my first issue too.

    By moving the buttons to their own UpdatePanel they are no longer affected by the timer and the javascript is not interrupted. I guess the issue with the javascript was that the buttons were reloaded in the UpdatePanel and that interrupted the script.