JavaScript的setTimeout()和setInterval()

1. setTimeout()方法

做用:在制定的毫秒數後調用函數或計算表達式javascript

語法:html

setTimeout(code,millisec)

實例:java

function timedMsg()
{
    var t=setTimeout("alert('5 seconds!')",5000)
}

...
<input type="button" value="Display timed alertbox!" onClick="timedMsg()">

2. setInterval()方法

做用:按照指定的週期(以毫秒計)來調用函數或計算表達式函數

語法:code

setInterval(code,millisec[,"lang"])

返回值:一個能夠傳遞給 Window.clearInterval() 從而取消對 code 的週期性執行的值htm

setInterval() 方法會不停地調用函數,直到 clearInterval() 被調用或窗口被關閉。由 setInterval() 返回的 ID 值可用做 clearInterval() 方法的參數。ip

實例:get

<html>
<body>

<input type="text" id="clock" size="35" />
<script language=javascript>
var int=self.setInterval("clock()",50)
function clock()
  {
  var t=new Date()
  document.getElementById("clock").value=t
  }
</script>
<button onclick="int=window.clearInterval(int)">Stop interval</button>

</body>
</html>
相關文章
相關標籤/搜索