js延迟执行函数

本文介绍了JavaScript中setTimeout和setInterval两种延迟执行函数的用法,并通过实例展示了如何结合使用它们来实现特定功能,如定时更新页面内容直至完成。

转自:http://zou-jiao.iteye.com/blog/1606872


在js中,延迟执行函数有两种:
setTimeoutsetInterval,用法如下:

setTimeout("test()","2000");  //2000毫秒后执行test()函数,只执行一次。
setInterval("test()","2000"); //每隔2000毫秒执行一次test()函数,执行无数次。
var interval = window.setInterval("test()","2000");
window.clearInterval(interval);     //停止执行setInterval循环。

当我们想让test()函数每隔2000毫秒执行一次,执行10000毫秒后停止执行时,可以用两者三者结合使用来实现。
var interval2 = window.setInterval("openit2()",2000);
setTimeout(function() {window.clearInterval(interval2);},10000);

带参方法执行延迟
setTimeout(function(){return executeQueryTask(data);},"10000");

例子:

<html>
  <head>    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script>
        var num = 3;
        window.onload = function(){
            var s = window.setInterval(function(){
                document.getElementById("s").innerHTML=num;
                num--;
                if(num<=0){
                    window.close();

                }
            }, 1000);            
        }
    </script>
    
  </head>
  <body>
        <h1><s:property value="msg"/></h1>
        <h2>本窗口在<span id="s">3</span>秒之后关闭!</h2>
        <input type=button value=关闭窗口  onclick="window.close();"  />
  </body>
</html>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值