这是我的代码:
var testStacks = new Array();
function test(elem) {
console.log(elem);
... asynch operations
}
testStacks.push(test("hello 0"));
testStacks.push(test("hello 1"));
testStacks.push(test("hello 2"));
testStacks.push(test("hello 3"));
testStacks.push(test("hello 4"));
// init first 3 functions
testStacks[0];
testStacks[1];
testStacks[2];
我想一次执行3个功能。因此hello 0
,hello 1
与hello 2
从头开始在一起。然后,一旦一个函数完成(他们执行异步操作),它就必须从数组中调用下一个(尚未执行)。等等...
似乎什么testStacks[0]
也没做,当我按下函数时,它将被执行。
我怎样才能做到这一点?(我想避免setInterval()
)。
一种简单的方法可以同时推送函数和参数。
var testStacks = new Array();
function test(elem) {
console.log(elem);
... asynch operations
}
testStacks.push({func: test, param: "hello 0"});
testStacks.push({func: test, param: "hello 1"});
testStacks.push({func: test, param: "hello 2"});
testStacks.push({func: test, param: "hello 3"});
testStacks.push({func: test, param: "hello 4"});
// init first 3 functions
testStacks[0].func(testStacks[0].param);
testStacks[1].func(testStacks[1].param);
testStacks[2].func(testStacks[2].param);
当然,可以用很多方法对此进行概括和清理,但是应该给您一个基本的想法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句