I am facing an issue with a script that sends an AJAX request every 10 seconds (technically 11) to the user. I have created a simple countdown from 10 to 0 that repeats continuously.
The countit
function is triggered after each AJAX request to reset the countdown.
Below is the code snippet:
function countit() {
var count = 10;
loading = setInterval(function() {
$(box).val("Will automatically check in " + count + "second(s), please DO NOT refresh the page yourself.");
count--;
if (count <= 0) {
clearInterval(loading);
}
}, 1000);
}
Everything works as expected, except for when a user leaves and returns to the page, the countdown goes into negative numbers and does not stop. It can be seen in this image:
https://i.sstatic.net/8K0sH.png
I am unsure if this is an issue with the code or JavaScript itself, but the counter behaves unexpectedly when the user remains on the page.