JavaScript setTimeout():行上的Type Error回调不是blob处的函数:回调不是函数



我正在创建一个延迟函数,它接受回调和以毫秒为单位的等待时间作为参数。delay应该返回一个函数,当调用该函数时,该函数将在执行之前等待指定的时间。在这里,我使用setTimeout((在执行函数之前设置一个计时器。

function delay (callback, time) {
function waitOnMe(...args) {
return setTimeout(time);
}
return waitOnMe;
}

我使用以下代码来测试上面的代码:

let count = 0;
const delayedFunc = delay(() => count++, 1000);
delayedFunc();
console.log(count);                                                  // should print '0'
setTimeout(() => console.log(count), 1000); // should print '1' after 1 second

然后我得到以下输出和错误消息(注意,第三个输出是在第二行出现后大约1秒生成的(。

0
Type Error on line callback is not a function at blob: callback is not a function
0

我想我得到这个错误是因为函数在指定的等待时间后没有从延迟返回以执行回调,但我不确定。

setTimeout的第一个参数应该是回调,第二个参数是延迟。其余参数用作回调的参数,可以通过排列语法传递回调。

function waitOnMe(...args) {
return setTimeout(callback, time, ...args);
}

最新更新