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

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -