Home > Web Front-end > JS Tutorial > body text

Lazy loading implementation code of js function_javascript skills

WBOY
Release: 2016-05-16 17:49:12
Original
1181 people have browsed it
Copy code The code is as follows:

//For non-delayed loading functions, conditional judgment will be performed every time it is called.
removefunctionHandler(target, eventType, handler) {
if(target.removeEventListener) {
target.removeEventListener(eventType,handler,false);
}else {
target.detachEvent( "on" eventType,handler);
}
}
//The delayed loading function will overwrite the original old function after the first call. The new function will be called again in the future and will not be called again. Make conditional judgments 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 preloading
//Conditional preloading ensures that all function calls take the same time. The cost is instrumentation when the script loads. Preloading is suitable for situations where a function will be used immediately and is used frequently throughout the page life cycle.

var addEventHandler = document.body.addEventListener ? function(target,eventType,handler) {
target.addEventListener(eventType,handler,false);
} : function(target,eventType,handler) ) {
target.attachEvent("on" eventType,handler);
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template