settimeout array javascript code example
Example 1: how to set a timeout on an array element
var ary = ['kevin', 'mike', 'sally'];
for(let i = 1; i <= ary.length; i++){
setTimeout(function(){
console.log(ary[i - 1]);
}, 5000 * i);
}
Example 2: how to set a timeout on an array element
function ArrayPlusDelay(array, delegate, delay) {
var i = 0
function loop() {
delegate(array[i]);
if (i++ < array.length - 1)
setTimeout(loop, delay);
}
setTimeout(loop, delay);
}
ArrayPlusDelay(['d','e','f'], function(obj) {console.log(obj)},1000)
Example 3: how to set a timeout on an array element
function ArrayPlusDelay(array, delegate, delay) {
var i = 0
var interval = setInterval(function() {
delegate(array[i]);
if (i++ >= array.length - 1)
clearInterval(interval);
}, delay)
}
ArrayPlusDelay(['x','y','z'], function(obj) {console.log(obj)},1000)