1

I have this code:

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

When I do this, I can't use break because it should be used in a for loop, which it isn't because I put it in a setTimeout function. I want to know how I can break out of the for loop and still delay the code within the setTimeout function.

2 Answers 2

1

Try this:

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

Didn't tested it.

Sign up to request clarification or add additional context in comments.

1 Comment

I could not vote you down with that nick... so I did it up :)
0

You could do this using a recursive function, like this:

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 );

Comments