gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:40:45 26 4
gpt4 key购买 nike

我正在尝试按照编写的确切顺序运行以下代码,没有任何内容跳过它之前的函数。有没有办法做到这一点?我知道使用 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();

最佳答案

如果你想使用 Promise 并摆脱全局变量,你可以这样做。这里没有真正需要使用 promise ,因为您可以在完成时让 roundOne 调用 roundTwo 等等,但使用 promise 的目的更通用,因为您可以通过使用 Promise 以任意顺序重用它们。

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);
<小时/>

而且,如果这些函数真的像看起来一样,那么您可能真的 DRY通过传递消息和对公共(public)代码块的限制,它们就像这样:

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]);
});
<小时/>

而且,这是一个不使用 promise 的 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]]);

关于javascript - 如何链接多个 $.done 以按特定顺序运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090766/

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