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

JS delayed loading (setTimeout) JS is loaded last_javascript skills

WBOY
Release: 2016-05-16 18:23:05
Original
1335 people have browsed it

First JS lazy loading

Copy code The code is as follows:


<script> <br> setTimeout("document.getElementById('my').src='include/common.php'; ",3000);//Delay 3 seconds <br> </script>

Under normal circumstances, setTimeout is used to achieve
The second JS is loaded last
Insert the following code where JS needs to be inserted:
Copy code The code is as follows:

LOADING...

Of course, that LOADING... you can change it to a small picture you like. It looks very AJAX effective.
Then insert at the bottom of the page:
Copy code The code is as follows:



js code
Copy code The code is as follows:

<script>L4EVER.innerHTML=AD_L4EVER.innerHTML;AD_L4EVER.innerHTML="";</script>


The standard syntax of setTimeout is: setTimeout(expression, time (milliseconds)) two parameters.

Here, focus on the call when the first parameter is a function, assuming it is a function.

1. Function without parameters:
function alertV(){ alert("000"); }

When the first parameter is not quoted ("" or ''), there is a one second delay:
setTimeout(alertV,1000);

When the first parameter is quoted, delay for one second:
setTimeout("alertV()",1000);


2. The function has parameters:

function alertV(event){ alert("keyCode=" event.keyCode); }

At this time it should be set to:
setTimeout(function(){alertV(event);},1000); Otherwise, it will prompt that the parameter is undefined.
setTimeout(function () {
    $("#id").html(datastr);
  }, 1000);
Copy after login

jquery is implemented through delay, such as

$("#id").delay(1000).html(datastr);
Copy after login

setTimeout('yourFunction()',5000); Execute yourFunction() after 5 seconds, only once
setInterval('yourFunction()',5000); Execute u
every 5 seconds
If setTimeout('yourFunction()',5000) is called again in yourFunction(), you can complete something similar to

The function of setInterval('yourFunction()',5000)

If anyone has a better and feasible method, thank you for sharing.

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