function throttling (throttle) and function jitter (debounce)

Source: Internet
Author: User

The throttle control function executes at a specific frequency
Debounce control functions are executed at a specific time interval

Usage Scenarios

Throttle

1. MouseMove event when dragging
2. Mousedown,keydown event in shooting game
3.window Scroll when updating styles, such as the follow-up effect

Simple implementation
var throttle = function (delay, action) {
var last = 0;
return function () {
var Curr = +new Date ();
if (Curr-last > Delay) {
Action.apply (this, arguments);
last = Curr;
}
};
};

Debounce

1. Automatic completion of text input (KeyUp), real-time verification
2. Resize the window to recalculate the style or layout

Simple implementation
var debounce = function (idle, action) {
var last;
return function () {
var ctx = this, args = arguments;
Cleartimeout (last);
last = SetTimeout (function () {
Action.apply (CTX, args);
}, idle);
};
};

Implementation of Throttle and debounce

Related JS library underscore.js,lodash.js,jquery-throttle-debounce.js and so on have its realization.

Reference: http://www.cnblogs.com/fsjohnhuang/p/4147810.html

function throttling (throttle) and function jitter (debounce)

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.