javascript infinite loop with delay
xxxxxxxxxx
var delay = 2000; // interval in ms
var myInterval = setInterval(function() {
// code to execute every `delay` milliseconds
}, delay);
// Call this to stop it
clearInterval(myInterval);
xxxxxxxxxx
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
setTimeout(function() { // call a 3s setTimeout when the loop is called
console.log('hello'); // your code here
i++; // increment the counter
if (i < 10) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 3000)
}
myLoop(); // start the loop
Run code snippet