Basic optimization ideas for window resize and scroll events

Source: Internet
Author: User

My colleague used the scroll event to load data in the project. As a result, IE is a tragedy. A simple optimization method is provided, with obvious results.

As long as the user changes the window size, the size of some internal elements will be re-calculated, which may cause the whole page to be re-rendered, resulting in a large amount of CPU consumption. For example, if you call the resize method, the user changes the window size and is triggered continuously. Earlier versions of IE may be suspended. The same is true for Windows scroll events. If you scroll the mouse or drag the scroll bar, scroll events will be triggered continuously. If there are many items to be processed, the earlier version of IE will also be suspended.

Basic optimization: within a certain period of time, only the resize event function is executed.
Copy codeThe Code is as follows:
Var resizeTimer = null;
$ (Window). on ('resize', function (){
If (resizeTimer ){
ClearTimeout (resizeTimer)
}
ResizeTimer = setTimeout (function (){
Console. log ("window resize ");
},400 );
}
);

The same is true for scroll event optimization.

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.