【发布时间】:2018-11-30 12:02:48
【问题描述】:
我已经使用 setTimeout 很长时间了,但是我无法解释我的节点 js 超时忽略等待时间。
这里是被指责的代码(在 Node 8.11.3 中):
//Here is the issue
socket.on('GameInput', function (input, state) {
setTimeout(socket.player.input, 10000, input, state);
});
//The player constructor is pretty standard
const _PLAYER = function(socket, name) {
//properties
this.input = function(input, state) {
//dosomestuff
io.emit('GameInput', this.name, input, state);
}
}
我只是在玩家输入上做一个假延迟(以测试可玩性),无论我写多少毫秒都没有效果。
编辑: 真正的问题是我没有正确重启我的节点服务器...... 好的代码确实是 setTimeout(()=>{socket.player.input(input, state);}, 10000); 我试过了,但没有重启效果就不是那么明显-__-
【问题讨论】:
-
你为什么要这样而不是这样传递参数:
setTimeout(socket.player.input(input, state), 10000);? -
感谢您的尝试,但我们不能在超时时使用被调用函数。我试图避开这个问题,没有根据文档调用它并传递参数,但它同样失败了。
标签: javascript node.js timeout