Applying delay between iterations of javascript for loop
for(i = 1; i < badge_arr.length; i++){
(function(i){
setTimeout(function(){
responseStr += badge_arr[i];
//Create growl notification
//badge info echoed back will be of the form
//Earned badge: name: description: imgSource
var badge_info = badge_arr[i].split(':');
var title = 'NEW BADGE UNLOCKED';
var text = 'You just unlocked the badge '+badge_info[0] +
': '+badge_info[1];
var img = badge_info[2];
createGrowl(title, text, img);
}, 1000 * i);
}(i));
}
Illustration:
for(i = 1; i <= 8; i++){
(function(i){
setTimeout(function(){
document.body.innerHTML += i + "<br/>"
}, 1000 * i);
}(i));
}
I prefer to use self-invoking function that receives a number of iterations:
(function loop(i) {
setTimeout(function () {
console.log('hello world'); // your code
if (--i) loop(i); // iteration counter
}, 5000) // delay
})(10); // iterations count