javascript - Killing a "personalized" Interval -
i've made system users can login website , play game requires interval timing. when user done playing want kill interval. while seems running fine, there wrong killing interval.
here problem whenever user done playing interval gets killed, not user playing everyone. might because i'm assigning variable interval , when user done playing game i'm killing interval
, right kill other intervals well?
here code i've written question,
var user; //this variable has info user. (its not empty) var usersplaying = []; socket.on('game', function(game) { if(game.type == "start"){ usersplaying.push({ user_id: user.id }); var game = setinterval(function(){ if(finduser(user.id) !== undefined){ console.log('second passed!'); }else{ clearinterval(game); //stop interval } }, 1000); }else if(game.type == "stop"){ console.log("user has decided quit playing game!"); usersplaying.splice(usersplaying.findindex(user => user === user.id), 1); //remove user playing } });
there might mistakes in there since rewritten , simplified code otherwise way hard me out.
anyways, how can make clears interval running specified person?
thanks!
the setinterval
call returns unique id. can use id clear interval timer afterwards:
var uniqueid = setinterval(function () { /* ... */ }, 1000);
later on ...
clearinterval(uniqueid);
will kill specific timer.
i suggest storing uniqueid each user inside usersplaying
array.
Comments
Post a Comment