In window object, there is a function setInterval which allows your to run a task repeatedly at an interval.


setInterval(doSomething; 100);


However, if the method last longer than the preset interval, it is not so efficient. We can use the follow function to make it more predictable.


loopTask(doSomething, 100);

function loopTask(fn, interval) {
   (function(){
       fn();
       setTimeout(arguments.callee, interval);
   })();
}