How do I correctly use setInterval and clearInterval to switch between two different functions?

You need to capture the return value from setInterval( … ) into a variable as that is the reference to the timer: var interval; var count = 0; function onloadFunctions() { countUp(); interval = setInterval(countUp, 200); } /* … code … */ function countUp() { document.getElementById(“here”).innerHTML = count; count++; if(count === 10) { clearInterval(interval); countUp(); … Read more

Are clearTimeout and clearInterval the same?

Actually I believe that we can make a fairly strong conclusion from the W3C spec (http://www.w3.org/TR/html5/webappapis.html#timers). It is not explicitly guaranteed but we have a lot of evidence that almost any reasonable implementation would have this behavior: 1) Timeouts and Intervals actually use the same underlying function: The setTimeout() method must return the value returned … Read more

How to clearInterval with unknown ID?

From quick test, all major browsers (latest Chrome, Firefox and IE) give pretty small numbers as the ID so just looping “blindly” over all possible numbers should work just fine: function ClearAllIntervals() { for (var i = 1; i < 99999; i++) window.clearInterval(i); } Full example: window.onload = function() { window.setInterval(function() { document.getElementById(“Tick”).innerHTML += “tick<br … Read more