【发布时间】:2014-01-20 10:19:47
【问题描述】:
我有这个代码:
$(function(){
var steps = ["download","unpack","install","installed"];
for(var i = 1; i <= steps.length; i++){
setTimeout(function(){
if(updater(steps[i]) === false) break; // if update fails
else{
var progress = (i / steps.length) * 100;
$("div#update div.progress div.progress-bar").animate({
width : progress+"%"
}).attr("aria-valuenow", progress);
}
, 5000)
}
if(steps.length === i){ // update is fully installed
alertBox("success", "Congratulations, a new version of the SocialTab 3 has been installed.", 31556952000);
}
else{ // update failed
alertBox("error", "<p>Update failed!</p><p>"+updateError+"</p>", 31556952000);
}
});
当我这样做时,我不能使用break,因为它应该在 for 循环中使用,而不是因为我将它放在 setTimeout 函数中。
我想知道如何跳出 for 循环并仍然延迟 setTimeout 函数中的代码。
【问题讨论】:
标签: javascript for-loop settimeout