简体   繁体   中英

Node.js with Socket.IO setTimeout not working

I'm doing a little test with canvas and Node.js , I have this in my server.js:

function updateAllClients() {
    io.sockets.emit('update', {x:x, y:y});
    x++;
    y++;
    t = setTimeout(updateAllClients, 100);
}
updateAllClients();

And in my client:

socket.on('update', function(data) {
    animate(data.x, data.y);
});

I've checked all the functions and they are working, but the setTimeout doesn't run...

it works for me when I put updateAllClients() inside the io.on('connection'...) function, something like that:

function updateAllClients() {
    io.socket.emit('update', {x:x, y:y});
    x++;
    y++;
    setTimeout(updateAllClients, 100);
}

io.sockets.on('connection', function (socket) {
    updateAllClients();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM