setTimeout与setTimeInterval均为window的函数,使用中顶层window一般都会省去,这两个函数经常稍不留神就使用错了。
setTimeout内的函数先不执行,隔一段时间后再执行,函数后面的数字是隔的时间,单位是毫秒(千分之一秒)
比如:
setTimeout(‘alert(“hello world!”)’, 4000);
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式,直到clearInterval()被调用或窗口被关闭。
比如:
<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 οnclick=”int=window.clearInterval(int)”>Stop interval</button>
setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象。可以使用本动作更新来自数据库的变量或更新时间显示。
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/114789.html<