JS several ways to delay latency loading

JS delayed loading helps and increase the loading speed of the page, the following is a few ways to delay loading:

1. Load time using the settimeout delay method

Delayed loading JS code, load online to leave more time

   function a () {$ .post ("/ LORD / login", { Name: username, pwd: password}, ​​function ()} ("hello");});} $ (Function () {settimeout ('a ()', 1000); // delay 1 second})   
2. Let JS last load

When introducing an external JS script file, if it is placed in HTML In the head, the JS script is loaded into the page before loading, and in the Body, then run the JavaScript code from the uploaded load order ~~~ So we can introduce the outside of JS. The file is placed at the bottom of the page, let JS finally introduced, speed page loading speed

3. The above method 2 will occasionally let you receive the “latency load JavaScript” warning of the Google page speed test tool. So the solution here will be a recommendation scheme from the Google Help page.

// These code should be placed before the
tag (close to the bottom of the HTML file)

function downloadjsatonLoad () {var element = document. CreateElement (“script”); element.src = “Defer.js “; document.body.appendChild (element);} if (window.addEventListener) window.addEventListener (” load “, downloadJSAtOnload, false); else if (window.attachEvent) window.attachEvent (” onload “, downloadJSAtOnload); Else window.onload;
 This code means that after the entire document is loaded, the external file "defer.js" is loaded.    Steps to use this paragraph:   
1). Copy the above code
2). Paste the code to the HTML tag (near the bottom of the HTML file)

3). Modify “Defer.js” for your external JS file name

4). Make sure your file path is correct. For example, if you only enter “Defer.js” Then, the “defer.js” file must be in the same folder with the HTML file.

Note: This code will load the specified external JS file until the document load is loaded. Therefore, those pages should not be normal. The JavaScript code that needs to be rely on is placed here. The JavaScript code should be divided into two groups. One group is the JavaScript code that is loaded immediately due to the page, and the other group is a JavaScript code that operates after the page is loaded (such as adding a Click event. Or other things). These needs to wait until the page is loaded and executed, then executed, it should be placed in an external file, and then it will be introduced.

The above is all the content of this article, I hope to learn to everyoneI have helpful, I hope everyone will support Tumi Clouds.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment