gpt4 book ai didi

javascript - 回调不是一个函数吗?

转载 作者:行者123 更新时间:2023-11-28 12:19:17 25 4
gpt4 key购买 nike

我知道这个问题已经被问过数百次了,但是我之前写过大量的回调函数,我想我只是对我的问题有点盲目/

我有一个功能:

function firstSend(){
client.write(Buffer.from([0x5C,0x57,0x31,0x32,0x33,0x34,0x2F]));

check(function(msg){
if(msg == true){
console.log("Lets go");
}
});
}

通过回调调用函数check

检查函数完成后返回 true:

function check(callback) {
let m;
if(message != null) m = message.trim();

if(m != "OK"){
setTimeout(check, 1000);
return;
}
return callback(true);
}

一切正常,直到它尝试执行回调,此时它告诉我它不是一个函数。

我已经记录了回调并将其记录为一个函数,所以我有点困惑

最佳答案

您没有在 setTimeout 中传递回调

setTimeout(function () {
check(callback)
}, 1000);

而不是

setTimeout(check, 1000);

或者,您也可以使用bind()

setTimeout(check.bind(null, callback), 1000);.

关于javascript - 回调不是一个函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42003552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com