`

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

阅读更多

一、setTimeout

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

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

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

 

二、setInterval

setInterval(表达式,交互时间)
则不一样,它从载入后,每隔指定的时间就执行一次表达式
用setInterval实现的自动变化显示随机数的效果:

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

<div id="DateTime">
    
</div>
</td>
</body> 
</html>

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics