Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throttle callback of jQuery event?

Tags:

jquery

Ok, so I do search like in google, you type text in input and it gives you entries instantly. But I don't like that. I use something like that $("TEXTINPUT").keyup(function() {. When the user types very fast, it connects to database many times. Is it possible, that we would connect to PHP file only when user stops typing for 1-2 seconds, but not instantly? I need to do that in jQuery. Thanks.

like image 685
good_evening Avatar asked Dec 03 '11 06:12

good_evening


People also ask

What is jQuery throttle Debounce?

jQuery throttle / debounce allows you to rate-limit your functions in multiple useful ways. 1k. (MIT OR GPL-2.0) licensed. Tags: jquery, throttle, debounce, ratelimit. 1.1.

How do I throttle JavaScript?

Implement a Throttle Function With Vanilla JavaScriptInitialize a variable to detect if the function has been called within the specified time. If the function has been called, pause the throttle function. If the function hasn't been called or is done running in the interval, rerun the throttle function.

What is the difference between Debouncing and throttling?

Throttle: the original function will be called at most once per specified period. Debounce: the original function will be called after the caller stops calling the decorated function after a specified period.

What's Debouncing and throttling concepts in JavaScript explain the scenarios with an example and how different is each?

Examples include window resizing and scrolling. The main difference between throttling and debouncing is that throttling executes the function at a regular interval, while debouncing executes the function only after some cooling period. Debouncing and throttling are not something provided by JavaScript itself.


2 Answers

If you use the Underscore Library it's as simple as this:

$("TEXTINPUT").keyup(_.throttle(function () {...}, 150));

Docs from the Underscore site:

throttle   _.throttle(function, wait)

Returns a throttled version of the function, that, when invoked repeatedly, will only actually call the wrapped function at most once per every wait milliseconds. Useful for rate-limiting events that occur faster than you can keep up with.

There is also the debounce function:

debounce   _.debounce(function, wait)

Calling a debounced function will postpone its execution until after wait milliseconds have elapsed since the last time the function was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving. For example: rendering a preview of a Markdown comment, recalculating a layout after the window has stopped being resized...

like image 161
Jordan S. Jones Avatar answered Oct 29 '22 15:10

Jordan S. Jones


Try :

var time_out;
$("TEXTINPUT").keyup(function(){
    clearTimeout(time_out);
    time_out = setTimeout(your_function, 500);
}

function your_function()
{
    /*CHECK DATABASE*/
}
like image 26
Yoann Avatar answered Oct 29 '22 15:10

Yoann