Reputation: 9111
My application is relying on a date comparison in order to take users "offline"
This is my code:
setInterval(function () {
for (var s in _client) {
if (_client.hasOwnProperty(s)) {
var obj = _client[s];
var lastVisit = new Date(_client[s].lastActive); // for example: 2013-10-8 9:52:24
var thirtySecs = 31000;
var now = new Date();
var thirtySecsAgo = new Date(now - thirtySecs);
if (lastVisit < thirtySecsAgo) {
// Should now remove user
// This does not always work for some reason
}
}
}
}, 15000)
The logic is that it should check each active clients lastActive property (which is being updated by a heartbeat). But sometimes the if
is not triggerd and therefore the user is not removed.
I have been unable to find why sometimes the user is not removed, even though more than 30 minutes have passed.
Any ideas what could be wrong and how i could make sure that old users are always removed?
Upvotes: 0
Views: 67
Reputation: 91
I suggest converting variables now, lastVisit and thirtySecs to milliseconds via getTime(), so it will be easier to compare them as unsigned integers:
var lastVisit = (new Date(_client[s].lastActive)).getTime(); //1381215144000
var now = (new Date()).getTime(); //1381225150208
var thirtySecs = 31*1000; //31000
var thirtySecsAgo = now - thirtySecs; //1381225119208
Upvotes: 0
Reputation: 664
Strange things sometimes happen with automatic cast. Try explicit conversion to timestamp values and use those.
var d = new Date();
var ts = d.valueOf();
// execute operations
Upvotes: 1