javascript - How to set an upper limit for waiting for a socket.on event? -
i trigger event
socket.emit('release now');
which after time may or may not trigger event (depending on user input).
socket.on('released', function(){ //do stuff });
but want wait maximum, say, 5 seconds. , if not, carry on //do stuff
anyway (with other minor things changed).
how do settimeout
?, or should using in first place? because can't think of how have them (socket.on('...
, setimeout
) communicate each other....
not sure if understand you're after ill take shot:
var eventtimeout = settimeout(function{ /* timeout code here */}, 5000); socket.emit('release now'); // emit fired , timer set socket.on('released', function() { cleartimeout(eventtimeout); // cancel our timeout since got released on time // stuff });
so
/* timeout code here */
will fire if released event not happen within 5 seconds.
Comments
Post a Comment