我有一个函数可以执行多个DDBB调用,所以它是异步的。
我需要调用该函数并检查其返回的JSON对象中的数据值(优胜者)。如果为true,则需要再次调用该函数,直到winner == false。
我不能使用,因为它不是异步的,我该怎么做?
someFunction(function(result) {
// if result.winner == true repeat
})
您可以再次调用相同的回调函数,直到condition为true
:
someFunction(function repeat(result) {
if (result.winner) {
someFunction(repeat);
}
});
查看下面的演示。
someFunction(function repeat(result) {
document.body.innerHTML += '<br>' + result.winner;
if (result.winner) {
someFunction(repeat);
}
});
var results = [true, true, true, false];
function someFunction(callback) {
setTimeout(function() {
callback({winner: results.shift()});
}, (Math.random() + 1) * 1000 | 0);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句