如何在以下函数中的代码执行完毕后调用一个只显示警告框的函数?这是代码运行的fiddle,现在我希望能够在runArray函数中完成代码之后调用下面的附加函数“isComplete”。
function runArray(arr, fn, delay) {
var index = 0;
var cnt = 0;
var i = 0;
// Called once for each array element
function next() {
if (index >= arr.length) {
return;
}
cnt = +arr[index];
i = 0;
loop();
}
// Represents a single iteration of what was previously a for loop
// Will either schedule the next iteration or move to the next element
function loop() {
if (i < cnt) {
fn(index, i++);
setTimeout(loop, delay); // delay before next iteration
}
else {
++index;
setTimeout(next, delay); // delay before moving to next element
}
}
if (arr.length) {
next();
}
}
function isComplete(){
alert('completed!');
}
答案 0 :(得分:0)
尝试添加额外参数fnComplete
并将其传递给该函数。然后在return语句之前调用它:
function runArray(arr, fn, delay, fnComplete) {
var index = 0;
var cnt = 0;
var i = 0;
// Called once for each array element
function next() {
if (index >= arr.length) {
fnComplete();
return;
}
调用该函数:
runArray(theArray, theFunction, 100, isComplete);