(function($){ $.fn.extend({ /** * Function to determine an action when the user * stops press a key in a DOM Object * useful for example (searchers) to not send * a query every time the user press a key, instead * this will do the action after of a keypress with * a given or default time. * * @param {function} funcion * @param {integer} tiempo * @returns {undefined} */ onstopwrite : function(funcion,tiempo){ var timerEscritura; if(!tiempo){ tiempo = 400; } this.keyup(function(e){ clearTimeout(timerEscritura); timerEscritura = setTimeout( function(){ funcion(tiempo); }, tiempo ); }); } }); })(jQuery); $(document).ready(function(){ $('#test').onstopwrite(function(){ alert("Sending request"); },1000); // every second });
<span>Do something after 1 second, when the user writes.</span> <input type="text" id="test" />