gpt4 book ai didi

javascript 超时退出 for 循环

转载 作者:行者123 更新时间:2023-11-30 17:40:16 25 4
gpt4 key购买 nike

我有这个代码:

$(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 函数中的代码。

最佳答案

试试这个:

$(function(){
function StepUpdate(step)
{
var steps = ["download","unpack","install","installed"];

if (steps[step] != undefined)
{
setTimeout(function(){
if(updater(steps[step]) === true)
{
var progress = (step / steps.length) * 100;
$("div#update div.progress div.progress-bar").animate({
width : progress+"%"
}).attr("aria-valuenow", progress);
StepUpdate(step + 1);
}
else
{
// update failed
alertBox("error", "<p>Update failed!</p><p>"+updateError+"</p>", 31556952000);
}
}, 5000);
}
else
{
alertBox("success", "Congratulations, a new version of the SocialTab 3 has been installed.", 31556952000);
}
}

StepUpdate(0);
});

没有测试过。

关于javascript 超时退出 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21231325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com