Once I created this plugin called bindDelay
for jQuery:
$.fn.bindDelay = function( eventType, eventData, handler, timer ) { if ( $.isFunction(eventData) ) { timer = handler; handler = eventData; } timer = (typeof timer === "number") ? timer : 300; var timeouts; $(this).bind(eventType, function(event) { var that = this; clearTimeout(timeouts); timeouts = setTimeout(function() { handler.call(that, event); }, timer); }); };
Used as a normal bind
method, but the last argument is the delay before the handler starts (in a mile of a second):
$("input").bindDelay('keyup', function() { $("textarea").text( $(this).val() ); }, 2000);
See the script: http://jsfiddle.net/c82Ye/2/
And you untie and run it, as usual:
$("input").unbind("keyup"); $("input").trigger("keyup");
source share