【问题标题】:javascript break out of for loop with timeoutjavascript 因超时而跳出 for 循环
【发布时间】: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


    【解决方案1】:

    试试这个:

    $(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);
    });
    

    没有测试过。

    【讨论】:

    • 我不能用那个昵称投票给你...所以我做到了:)
    【解决方案2】:

    您可以使用递归函数来做到这一点,如下所示:

    var steps = ["download","unpack","install","installed"];
    
    function doUpdate( index ) {
        if( updater(steps[index]) === false) {
            alertBox( "error", "<p>Update failed!</p><p>"+updateError+"</p>", 31556952000 ); // if update fails
        }
        else{
            var progress = (index / steps.length) * 100;
            $( "div#update div.progress div.progress-bar" ).animate( {
                width : progress + "%"
            } ).attr("aria-valuenow", progress);
        }
    
        if( steps.length === i ) { 
            // update is fully installed
            alertBox("success", "Congratulations, a new version of the SocialTab 3 has been installed.", 31556952000);
        }
        else {
            doUpdate( index + 1 )
        }
    }
    
    doUpdate( 0 );
    

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2021-08-31
      相关资源
      最近更新 更多