Window resize and Scroll event's basic optimization idea _javascript skill

Source: Internet
Author: User
Colleagues in the project using the scroll event to load data, the result of IE tragedy. A simple optimization method is given, and the effect is obvious.

As long as the user changes the window size, the internal element size will be recalculated, which may cause the entire page to be rendered again, resulting in a large amount of CPU consumption. For example, call resize method, the user changes the window will be constantly triggered, the lower version of IE may fall into suspended animation state. Window Scroll event is also the case, the mouse scroll or drag the scroll bar, will not stop the trigger scroll events, if the processing of more things, the lower version of IE will also fall into suspended animation state.

Basic idea of optimization: within a certain period of time, only one Resize event function is executed.
Copy Code code as follows:

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

Scroll event optimization is similarly.

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.