Is there any way to call a function periodically in JavaScript?

coderex picture coderex · Aug 3, 2009 · Viewed 104k times · Source

Is there any way to call a function periodically in JavaScript?

Answer

zombat picture zombat · Aug 3, 2009

You want setInterval():

var intervalID = setInterval(function(){alert("Interval reached");}, 5000);

The first parameter to setInterval() can also be a string of code to be evaluated.

You can clear a periodic function with:

clearInterval(intervalID);