在 javascript for 循环的迭代之间应用延迟 [英] Applying delay between iterations of javascript for loop
本文介绍了在 javascript for 循环的迭代之间应用延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 jQuery 或下划线对 javascript for 循环的连续迭代应用延迟?我的页面上有一个 for 循环,当用户满足某些条件时,我用它来弹出咆哮通知,如果有多个条件,我想错开咆哮通知,而不是同时弹出几个.这是有问题的循环:
Is it possible to apply a delay to successive iterations of a javascript for-loop using jQuery or underscore? I have a for-loop on my page that I am using to pop up growl notifications when users fulfill certain conditions and if there are multiple conditions I would like to stagger the growl notifications instead of popping up several at the same time. Here is the loop in question:
var badge_arr = response.split("Earned badge:");
//Start at 1 so I'm not getting everything before the first badge
for(i = 1; i < badge_arr.length; i++){
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);
}
推荐答案
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));
}
插图:
for(i = 1; i <= 8; i++){
(function(i){
setTimeout(function(){
document.body.innerHTML += i + "<br/>"
}, 1000 * i);
}(i));
}
这篇关于在 javascript for 循环的迭代之间应用延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文