在我将 node.js 更新到版本 8.11.3 之前,我的代码一直有效
现在,当我尝试使用 setTimeout 调用函数时,总是会出现错误“回调参数必须是一个函数”。
function testFunction(itemid, price) {
var url = 'https://example.com';
var options = {
method: 'get',
url: url
}
request(options, function (err, res, body) {
var response = JSON.parse(body);
if(response.status == 'fail'){
setTimeout(testFunction(itemid, price), 100);
}
})
}
最佳答案
setTimeout
的回调参数必须是一个函数。像这样写。未经测试,但应该可以。
function testFunction(itemid, price) {
var url = 'https://example.com';
var options = {
method: 'get',
url: url
}
request(options, function (err, res, body) {
var response = JSON.parse(body);
if(response.status == 'fail'){
setTimeout(function () {
testFunction(itemid, price);
}, 100);
}
})
}
关于javascript - setTimeout - 回调参数必须是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51685383/