Implementation Code for delayed loading of js functions _ javascript tips-js tutorial

Source: Internet
Author: User
Delayed loading function. After the first call, it will overwrite the original old function, and then call the new function again, so it will not judge the conditions to improve efficiency. The Code is as follows:


// For non-delayed loading functions, conditional judgment is performed each call.
Function removeHandler (target, eventType, handler ){
If (target. removeEventListener ){
Target. removeEventListener (eventType, handler, false );
} Else {
Target. detachEvent ("on" + eventType, handler );
}
}
// Delayed loading function. After the first call, it will overwrite the original old function, and then call the new function again, so it will not judge the condition to improve efficiency.
Function addHandler (target, eventType, handler ){
If (target. addEventListener ){
AddHandler = function (target, eventType, handler ){
Target. addEventListener (eventType, handler, false );
}
} Else {
AddHandler = function (target, eventType, handler ){
Target. attachEvent ("on" + eventType, handler );
}
}
AddHandler (target, eventType, handler );
}


// Conditional Preload
// Make sure that all functions are called at the same time through conditional pre-loading. The cost is to check when the script is loaded. Pre-loading is suitable for scenarios where a function will be used immediately and is frequently used throughout the page lifecycle.

Var addEventHandler = document. body. addEventListener? Function (target, eventType, handler ){
Target. addEventListener (eventType, handler, false );
}: Function (target, eventType, handler ){
Target. attachEvent ("on" + eventType, handler );
}

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.