Skip to content

Instantly share code, notes, and snippets.

@yuksbg
Created March 30, 2011 13:57
Show Gist options
  • Save yuksbg/f586035a9448702de9a0 to your computer and use it in GitHub Desktop.
Save yuksbg/f586035a9448702de9a0 to your computer and use it in GitHub Desktop.
jquery delay timer
function delayTimer(delay){
var timer;
return function(fn){
timer=clearTimeout(timer);
if(fn)
timer=setTimeout(function(){
fn();
},delay);
return timer;
}
}
// USage
<input type="text" value="" />
<a href="">Stop Timer</a>
var inputDelayer=delayTimer(500);
$('input').keyup(function(){
inputDelayer(function(){
alert('user has stopped typing');
});
});
$('a').click(function(){
//both of these clear the timer
clearTimeout(inputDelayer());
inputDelayer();
//both of these clear the timer
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment