I have been using setTimeout for a long time nevertheless I can not explain my node js timeout ignoring the wait time.
Here the blamed code (in 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);
}
}
I am just making a fake lag on the player inputs (to test play-ability) and whatever milliseconds I write there is no effect.
Edit : The real problem is that i do not restart my node server correctly ... The good code is indeed setTimeout(()=>{socket.player.input(input, state);}, 10000); I tried it but without restart the effect is not such visible -__-
You need to wrap the function in the timeout in a function call, otherwise you are calling the function, not passing it through to be completed when the timeout ends.
Something like this:
socket.on('GameInput', function (input, state) {
setTimeout(function() { socket.player.input(input, state) }, 10000);
});