Window. onresize multiple triggers _ javascript skills

Source: Internet
Author: User
Window is used. onresize, but it is found that the status on the page is always incorrect after each onresize event. Let's share with you the solution to the onresize event multiple times before doing an extension, you must ensure that the page is displayed normally when changing the window size, so the window is used. onresize found that the status on the page is always incorrect after each onresize event. Later, it was found that the onresize event triggered multiple searches, So I collected a solution on the Internet and sorted it out.
//
The number of onresize event triggers varies with different browsers. safari, opera, and firefox are all one time (only one version is used for testing, and both are newer );
// Ie6 is triggered twice under quirk, and the standard is triggered three times. ie7 and 8 are both triggered twice in quirk and standard.

The Code is as follows:


Window. onresize = function (){
Console. log ('Hello World ');
}
Onresize does not matter how many times it is triggered. The most important solution is to call the onresize function once when multiple times are triggered.
//
// The Word "debounce" does not know how to translate it. I am a non-specialist and dare not translate it easily, so that I can not smile generously. :)
//
Var debounce = function (func, threshold, execAsap ){
Var timeout;
Return function debounced (){
Var obj = this, args = arguments;
Function delayed (){
If (! ExecAsap)
Func. apply (obj, args );
Timeout = null;
};
If (timeout)
ClearTimeout (timeout );
Else if (execAsap)
Func. apply (obj, args );
Timeout = setTimeout (delayed, threshold || 100 );
};
}


// The code is not written by me.
// Code Description:
Debounce accepts three parameters, and the last two are optional. The first parameter is the debounce function, the second parameter is the debouce interval, and the third parameter is the end of the execution function at the beginning of the time period;
Debounce returns the packaged function. The interval between two calls of this function is at least threshold, and the calls smaller than the threshold Interval start timing again (the interval between the two calls );
Replace clearTimeout (timeout) with timer = null. The returned function has at least two execution intervals of threshold, and CALLS smaller than threshold will start timing again (the interval of two calls );
// Solve multiple onresize calls

The Code is as follows:


Window. onresize = debounce (function (){
Alert ('Hello World ');
}, 100, true)


// In auto-completion, debounce is also used to reduce the number of requests to the server. ajax is sent only when the consecutive key-breaking interval is greater than a certain value.
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.