Analysis of throttle and debounce in JavaScript-basic knowledge

Source: Internet
Author: User
Tags diff time interval

Throttle

What we are talking about here is the meaning of function throttling throttle. And the popular point is that the function calls the frequency controller, is the continuous execution time interval control. The main application of the scene such as:

1. Mouse movement, MouseMove events
2.DOM element dynamic Positioning, Window object resize and scroll events

Some people image of the incident as described above as a machine gun fire, throttle is the trigger of a machine gun, you do not pull the trigger, it has been shooting. The same is true of the events that we use when we develop, and the events are triggered when you do not release the mouse. For example:

Copy Code code as follows:

var resizetimer=null;
$ (window). On (' Resize ', function () {
if (Resizetimer) {
Cleartimeout (Resizetimer)
}
Resizetimer=settimeout (function () {
Console.log ("Window resize");
},400);

Debounce

Debounce and throttle are similar, and the call method is executed when the debounce is free when the time must be greater than or equal to a certain value. Debounce is the interval control of idle time. For example, we do autocomplete, then we need to control the input text when the method time interval. In general, the first character entered immediately begins the call, repeating the method of execution based on a certain interval of time. It is especially useful for abnormal input, such as holding down a certain build.

Debounce main application scenarios such as:
Text input KeyDown events, KeyUp events, such as doing AutoComplete

There are many ways to do this online, such as underscore.js to encapsulate throttle and debounce. jquery also has a throttle and debounce plugin: jquery throttle/debounce, all of the principles of the same, the implementation of the same function. and a throttle and debounce control function that you've been using again:

Copy Code code as follows:

/*
* Frequency Control returns the function continuous call, the FN execution frequency is limited to each time executes once
* @param the function that the FN {function} needs to call
* @param delay {Number} delay time, per millisecond
* @param immediate {bool} The function to pass false binding to the immediate parameter executes first, not delay.
* @return {function} actually calling functions
*/
var throttle = function (Fn,delay, immediate, debounce) {
var Curr = +new Date (),//Current event
Last_call = 0,
last_exec = 0,
Timer = NULL,
diff,//Time lag
context,//context
Args
exec = function () {
Last_exec = Curr;
Fn.apply (context, args);
};
return function () {
curr= +new Date ();
Context = this,
args = arguments,
diff = Curr-(debounce last_call:last_exec)-delay;
Cleartimeout (timer);
if (debounce) {
if (immediate) {
Timer = settimeout (exec, delay);
else if (diff >= 0) {
exec ();
}
} else {
if (diff >= 0) {
exec ();
else if (immediate) {
Timer = settimeout (exec,-diff);
}
}
Last_call = Curr;
}
};

/*
* Idle control returns function Continuous call, the idle time must be greater than or equal to DELAY,FN before executing
* @param a function to be called by the FN {function}
* @param delay {Number} idle time
* @param immediate {bool} The function to pass false binding to the immediate parameter executes first, not delay.
* @return {function} actually calling functions
*/

var debounce = function (FN, delay, immediate) {
Return throttle (FN, delay, immediate, true);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.