I want to execute a function like 2 seconds after a user has finished typing in the textbox. If they continue to type after 1 second, the delay time is reset back to 2.
It should function something similar to an autocomplete box.
I know of 2 events: change and keyup. The problem I have with change is that the textbox has to loose focus for it to be triggered. for keyup, what if they use the mouse to paste a text?
Could I be helped here?
jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.
To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.
To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.
There's the HTML5 oninput event, supported by all the current major browsers and can be worked into IE 8 and lower:
$("#myInput").bind("input", function () { // ... }) A very simple cross browser approach would be
$("#myInput").bind("input propertychange", function (evt) { // If it's the propertychange event, make sure it's the value that changed. if (window.event && event.type == "propertychange" && event.propertyName != "value") return; // Clear any previously set timer before setting a fresh one window.clearTimeout($(this).data("timeout")); $(this).data("timeout", setTimeout(function () { // Do your thing here }, 2000)); }); This would make the event fire twice in IE 9 (one for propertychange, one for input), but it doesn't matter because of the nature of the event handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With