If you want to call a function at a regular interval, you should use setInterval().
var myFunction = function() {};
setInterval(myFunction, 1000);
If you ever need to stop a function from being called forever, setInterval () returns an identifier that you can also use to stop the timer. Here is an example.
var myFunction = function() {};
var timer = setInterval(myFunction, 1000);
clearTimeout(timer);
source
share