You do not know the function of throttling, improve your LJS performance!

Source: Internet
Author: User

The browser's Dom computing processing is very CPU-intensive, occupy memory, which is very unfriendly to our development, such as the onresize event of IE browser can be counted thousands of times when the user slightly drag the window, even higher frequency directly let the browser crash ...

All the students in the handwritten carousel are aware of the existence of a throttle valve, so is the function throttle (reference elevation):

var processor = {

Timeoutid:null,

The method of actual processing

Performprocessing:function () {

Code that is actually executed

},

Methods for initializing processing calls

Process:function () {

Cleartimeout (This.timeoutid);

var that=this;

This.timeoutid=settimeout (function () {

That.performprocessing ();

},100);

}

};

The basic idea is that a function that executes continuously is forced to execute every 100 milliseconds

The following is simplified using the throttle () function, which automatically sets and clears the timer:

Function Throttle (Method,context) {

Cleartimeout (Method,tid);

Method.tid=settimeout (function () {

Method.call (context);

},100);

}

How to use it? It's so simple and rude.

function MyFunction () {

Your idea

}

Window.onresize = function () {

Throttle (myFunction);

};

You do not know the function of throttling, improve your LJS performance!

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.