setInterval(),setTimeout(),location.reload(true)

1,setInterval()

setInterval()方法可以按照指定的周期来调用函数或表达式,他会不停地调用函数,直到调用clearInterval()方法或窗口关闭。由setInterval()返回的ID值,可用作clearInterval()的参数。

语法:

var id = setInterval(code,millisec);

clearInterval(id);

实例:

 1 <html>
 2 <body>
 3 
 4 <input type="text" id="clock" size="35" />
 5 <script language=javascript>
 6 var int=self.setInterval("clock()",50)
 7 function clock()
 8   {
 9   var t=new Date()
10   document.getElementById("clock").value=t
11   }
12 </script>
13 </form>
14 <button onclick="int=window.clearInterval(int)">
15 Stop interval</button>
16 
17 </body>
18 </html>
View Code

2,setTimeout()

setTimeout()方法用于在指定的时间后调用函数或表达式,他只执行一次code。

语法:

SetTimeout(code,millisec);

setTimeout()只调用一次code。如果多次调用,需要使用setinterval()或code自己调用setTimeout()。

实例:

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function timedMsg()
 5 {
 6 var t=setTimeout("alert('5 seconds!')",5000)
 7 }
 8 </script>
 9 </head>
10 
11 <body>
12 <form>
13 <input type="button" value="Display timed alertbox!"
14 onClick="timedMsg()">
15 </form>
16 <p>Click on the button above. An alert box will be
17 displayed after 5 seconds.</p>
18 </body>
19 
20 </html>
View Code

3,reload(true)

reload()方法用于重新加载当前文档。
location.reload(force);
如果参数force为空,或为false,它会用HTTP头If-Modified-Since来检测服务器上的文档是否改变,如果文档改变,reload()会再次下载该文档,否则直接从缓存中装载文档。与用户单击浏览器的刷洗按钮效果是一样的。
如果参数force为true,那么不论文档是否修改,肯定从服务器下载该文档。这与用户按住Shift键,同时单击刷新按钮一样的。
实例:

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function reloadPage()
 5   {
 6   window.location.reload()
 7   }
 8 </script>
 9 </head>
10 
11 <body>
12 <input type="button" value="Reload page"
13 onclick="reloadPage()" />
14 </body>
15 
16 </html>
View Code
原文地址:https://www.cnblogs.com/usa007lhy/p/4159683.html