js中的setTimeout与setTimeInterval

来源:互联网 发布:拍摄淘宝服装图片技巧 编辑:程序博客网 时间:2024/06/07 11:06

setTimeout与setTimeInterval均为window的函数,使用中顶层window一般都会省去,这两个函数经常稍不留神就使用错了。

setTimeout内的函数先不执行,隔一段时间后再执行,函数后面的数字是隔的时间,单位是毫秒(千分之一秒)

比如:

setTimeout('alert("hello world!")', 400);

setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式,直到clearInterval()被调用或窗口被关闭。

比如:
<!DOCTYPE html>
 
<html>
 
<body>
 
<form>
 
<input type="text" id="clock" size="35" />
 
<script>
 
var int=self.setInterval("clock()",50)
 
function clock(){var t=new Date()
 
document.getElementById("clock").value=t
 
}
 
</script>
 
</form>
 
<div id="clock"></div>
 
<button onclick="int=window.clearInterval(int)">Stop interval</button>
 
</body>
 
</html>
setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象。可以使用本动作更新来自数据库的变量或更新时间显示。

0 0
原创粉丝点击