Better JS Interval
// Variant 0: Basic
var speed = 500,
iterations = 5,
index = 0;
(function interval () {
// Do stuff...
if (index++ !== iterations) setTimeout(interval, speed);
})();
// Variant 1: Wait for code execution to be possible
var speed = 100;
(function interval () {
try {
// code that throws an error message
} catch (e) {
setTimeout(interval, speed);
}
})();