I'm making a module named rooms.js
for my game in socket.io
and canvas
, and I've a function to sync with the client the users data as an object, but setInterval
is not working on my function Rooms.Listener()
, the client only get the data 4 times with setInterval
at 1ms , but only one time with 10ms .
Code:
Listener: function() {
setInterval(function() {
// send data to client every 1ms
Rooms.ListUsers();
}, 1);
},
ListUsers: function() {
for(var roomID in Rooms.Obj) {
var room = Rooms.Obj[roomID];
// send users data to client
room.users.forEach(function(uid) {
var socketID = users.getSocketIDbyId(uid);
var data = Rooms.getUsersInRoomData(roomID);
fiveSocket.emitClient(socketID, headers.roomUsers, data);
});
}
},
getUsersInRoomData: function(roomID) {
var room = Rooms.Obj[roomID];
var obj = {};
room.users.forEach(function(uid) {
var user = users.Obj[uid];
obj[uid] = {
username: user.username,
position: user.position,
figure: user.figure
};
});
return obj;
},
Where's the problem? Thanks
Double check your methods separately. The code itself should work, problem may be inside your inner methods.
var Rooms = { Listener: function() { setInterval(function() { Rooms.ListUsers(); }, 1000); // change to 1ms if necessary }, ListUsers : function() { document.body.innerHTML += Date.now() + "<br />"; } }; Rooms.Listener();
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.