我有一个回调数组,因此每个回调都会在工作完成时执行另一个回调。例子:
var queue = [
function (done) {
console.log('Executing first job');
setTimeout(done, 1000); // actually an AJAX call here
// I can't call abort() for the request
},
function (done) {
console.log('Executing second job');
setTimeout(done, 1000);
// also AJAX call here
// it is made from a third-party library that I can't change
// this means that I can't call abort() on it
},
function (done) {
console.log('Executing third job');
setTimeout(done, 1000);
},
function (done) {
console.log('Executing fourth job');
setTimeout(done, 1000);
} // and so forth
];
var proceed = true;
(function dequeue() {
var fn = queue.shift();
proceed && fn instanceof Function && fn(dequeue);
})();
这对我来说很好,只是为了加快一切,我最好一次启动四个回调,同时仍然能够通过proceed
从其他地方更改标志来停止进一步的执行。我怎么做?
我在这个项目上使用了最新版本的 jQuery,所以如果库中有任何东西可以帮助完成这项任务,我会使用它。一切都发生在浏览器中。