Heim  >  Artikel  >  Web-Frontend  >  JS延迟加载(setTimeout) JS最后加载_javascript技巧

JS延迟加载(setTimeout) JS最后加载_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:23:051300Durchsuche

第一 JS延迟加载

复制代码 代码如下:


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

一般情况下都是利用setTimeout来实现
第二 JS最后加载
在需要插入JS的地方插入以下代码:
复制代码 代码如下:

LOADING...

当然,那个LOADING…你可以换成自己喜欢的小图片。看起来很有AJAX效果呢。
然后在页面最底端插入:
复制代码 代码如下:



js代码
复制代码 代码如下:

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


setTimeout的标准语法是: setTimeout(表达式,时间(毫秒)) 两个参数.

在这里着重记下第一个参数是一个 function时的调用,假设是一个函数.

1.函数无参:
function alertV(){ alert("000"); }

当第一个参数不加引号("" 或 '')时,延迟一秒:
setTimeout(alertV,1000);

当第一个参数加上又引号时,延迟一秒:
setTimeout("alertV()",1000);


2. 函数有参:

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

此时应设置成:
setTimeout(function(){alertV(event);},1000); 不然会提示参数未定义.
setTimeout(function () {
    $("#id").html(datastr);
  }, 1000);

jquery的通过delay实现,例如

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

setTimeout('yourFunction()',5000); 5秒后执行yourFunction(),只执行一次
setInterval('yourFunction()',5000); 每隔5秒执行一次u

如果在yourFunction()中再次调用了setTimeout('yourFunction()',5000),可以完成类似于

setInterval('yourFunction()',5000)的功能

要是哪位还有好的可行方法,谢谢共享.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn