jQuery - 如何链接多个$ .done以按特定顺序运行函数

时间:2014-08-02 01:22:40

标签: javascript jquery asynchronous javascript-events

我正在尝试按照编写的确切顺序运行以下代码,而没有跳过它之前的函数。有没有办法做到这一点?我知道使用jQuery的$ .done()在这里很有用,但我不太清楚如何继续,所以请帮助我们。这是代码。

function roundOne () {          
    setTimeout(function () { 
        terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ 'found.');
        j++;                     
        if (j < numberOfRowsToFind) {            
            roundOne();            
        }                        
    }, Math.floor((Math.random() * (1500)) + (70 * j)))
}
roundOne(); 

j = 0;
function roundTwo () {          
    setTimeout(function () { 
        terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ ' found.');
        j++;                     
        if (j < numberOfRowsToFind) {            
            roundTwo();            
        }                        
    }, Math.floor((Math.random() * (1500)) + (70 * j)))
}
roundTwo();

j = 0;
function roundThree () {          
    setTimeout(function () { 
        terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ ' found.');
            j++;                     
            if (j < currentSrgValues[0][1]) {            
                roundThree();            
            }                        
    }, Math.floor((Math.random() * (1500)) + (70 * j)))
}
roundThree();          

laddaCalculateButton.stop();

2 个答案:

答案 0 :(得分:2)

如果你想使用promises并摆脱全局变量,你可以这样做。这里没有真正需要使用承诺,因为你可以在完成后roundOne调用roundTwo,等等,但是使用promises是一个更通用的目的,因为你然后可以使用promises以任何顺序重用这些。

function roundOne () {       
    var def = $.Deferred();   
    var j = 0;

    function run() {
        setTimeout(function () { 
            terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ 'found.');
            j++;                     
            if (j < numberOfRowsToFindInRoundOne) {            
                run();            
            } else {
               // done with roundOne()
               def.resolve();
            }
        }, Math.floor((Math.random() * (1500)) + (70 * j)))
    }
    run();
    return def.promise();
}

function roundTwo () {          
    var def = $.Deferred();   
    var j = 0;

    function run() {
        setTimeout(function () { 
            terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ ' found.');
            j++;                     
            if (j < numberOfRowsToFindInRoundOne) {            
                run();            
            } else {
                // done with roundTwo
                def.resolve();
            }                       
        }, Math.floor((Math.random() * (1500)) + (70 * j)))
    }
    run();
    return def.promise();
}

function roundThree () {          
    var def = $.Deferred();   
    var j = 0;

    function run() {
        setTimeout(function () { 
            terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ ' found.');
                j++;                     
                if (j < currentSrgValues[0][1]) {            
                    run();            
                } else {
                    // done with roundThree
                    def.resolve();
                }
        }, Math.floor((Math.random() * (1500)) + (70 * j)))
    }
    run();
    return def.promise();
}

// then you sequence them like this
roundOne().then(roundTwo).then(roundThree);

而且,如果这些函数与它们看起来真的一样,那么通过将msg和限制传递给一个公共代码块,你可能真的DRY它们很像这样:

function runRound(msg, limit) {
    var def = $.Deferred();   
    var j = 0;

    function run() {
        setTimeout(function () { 
            terminal.echo(msg.replace("%d", j+1));
            j++;                     
            if (j < limit) {            
                run();            
            } else {
               // done with round
               def.resolve();
            }
        }, Math.floor((Math.random() * (1500)) + (70 * j)))
    }
    run();
    return def.promise();    
}

runRound('Lambda and mu row checks passed.  Row %d found.', numberOfRowsToFindInRoundOne)
    .then(function() {
       return runRound('Lambda and mu row checks passed.  Row %d found.', numberOfRowsToFindInRoundOne);
    }).then(function() {
       return runRound('Lambda and mu row checks passed.  Row %d found.', currentSrgValues[0][1]);
    });

而且,这是一个不使用承诺的DRY版本:

function runRounds(msg, limits) {
    var j = 0;
    var limit = limits.shift();

    function run() {
        setTimeout(function () { 
            terminal.echo(msg.replace("%d", j+1));
            j++;                     
            if (j < limit) {            
                run();            
            } else {
                // done with round - if more to go, run again
                if (limits.length) {
                    runRounds(limits);
                }

            }
        }, Math.floor((Math.random() * (1500)) + (70 * j)))
    }
    run();
}

runRounds('Lambda and mu row checks passed.  Row %d found.', [numberOfRowsToFindInRoundOne, numberOfRowsToFindInRoundOne, currentSrgValues[0][1]]);

答案 1 :(得分:1)

您可以定义上面的所有函数,使它们运行上一个函数。然后你会打电话给roundOne(),你就会完成。

function roundOne () {          
    setTimeout(function () { 
        terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ 'found.');
        j++;                     
        if (j < numberOfRowsToFind) {            
            roundOne();            
        }  else {
            j = 0; // finished, set j=0 and call roundTwo  
            roundTwo();  
        }               
    }, Math.floor((Math.random() * (1500)) + (70 * j)))
}

function roundTwo () {          
    setTimeout(function () { 
        terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ ' found.');
        j++;                     
        if (j < numberOfRowsToFind) {            
            roundTwo();            
        } else {
            j = 0; // finished, set j=0 and call roundThree
            roundThree();     
        }                   
    }, Math.floor((Math.random() * (1500)) + (70 * j)))
}

function roundThree () {          
    setTimeout(function () { 
        terminal.echo('Lambda and mu row checks passed.  Row ' +(j+1)+ ' found.');
            j++;                     
            if (j < currentSrgValues[0][1]) {            
                roundThree();            
            } else {
                laddaCalculateButton.stop(); // finished all the three rounds        
            }            
    }, Math.floor((Math.random() * (1500)) + (70 * j)))
}

j = 0;
roundOne();