javascript - JQuery timeout function -


i trying use jquery/javascript following having trouble finding information how done.

i want create function when called, wait 2 seconds response user (a keypress example). if response collected anytime within 2 seconds, event executed while if there no keypress withing 2 seconds, event b executed.

function waitforinput { if(response within 2 second timeout?) event executed else if (timed out) event b executed } 

try this

var timeout;  $('#input').on('keyup', function () {     if (timeout) {        cleartimeout(timeout);        $('#div').removeclass('a');  // corresponds event     }     timeout = settimeout(function () {         $('#div').addclass('a');  // corresponds event b     },1000);     console.log(timeout) }); 

check fiddle


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -