roll()函数运行良好,但是当我试图获得回调时它会抛出错误:回调不是函数
var speed = 300; function roll(callback) { if (typeof callback === "function") { console.log('callback is function!'); //yes } if (speed < 1000) { speed += 50; setTimeout(roll, 1000); //increase speed } else if (speed >= 1000) { console.log('finished'); return callback(true); //problem here? } } roll(function(callback) { console.log(callback); //callback is not a function });
小智.. 5
问题的根本原因在于:setTimeout(roll, 1000)
.
roll
被调用但 没有 callback
功能setTimeout
.
var speed = 300;
function roll(callback) {
console.log('callback', callback);
if (speed < 1000) {
speed += 50;
setTimeout(function() {
roll(callback); //pass the callback
}, 1000); //increase speed
} else {
console.log('finished');
callback(true); //removed un-wanted `return`
}
}
roll(function(result) { //renamed parameter
console.log(result);
});
问题的根本原因在于:setTimeout(roll, 1000)
.
roll
被调用但 没有 callback
功能setTimeout
.
var speed = 300;
function roll(callback) {
console.log('callback', callback);
if (speed < 1000) {
speed += 50;
setTimeout(function() {
roll(callback); //pass the callback
}, 1000); //increase speed
} else {
console.log('finished');
callback(true); //removed un-wanted `return`
}
}
roll(function(result) { //renamed parameter
console.log(result);
});