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) });
Comments
Post a Comment