Jscript中window.setInterval和window.setTimeout的区别

来源:互联网 发布:智能家电控制软件 编辑:程序博客网 时间:2024/05/20 08:24

setTimeout(表达式,延时时间)
在执行时,是在载入后延迟指定时间后,去执行一次表达式,记住,次数是一次

用setTimeout实现的自动变化显示随机数的效果:

<html>
<head>
<script>
window.onload=sett;
function sett()
{
document.body.innerHTML=Math.random();
setTimeout("sett()",500);
}
</script>
</head>
<body>
</body>
</html>


[setInterval]
setInterval(表达式,交互时间)
则不一样,它从载入后,每隔指定的时间就执行一次表达式

用setInterval实现的自动变化显示随机数的效果:

<html>
<head>
<script>
function sett()
{
document.body.innerHTML=Math.random();
}
setInterval("sett();", 500); 
</script>
</head>
<body>
</body>
</html>

原创粉丝点击