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

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -