Web - JS中的自动进程(setTimeout和setInterval)

setTimeout (需要执行的函数,延迟的时间ms);
比如 :

setTimeout(function(){
	alert("Hello World!");
},3000);

就会在3s后输出Hello World;
setInterval(需要执行的函数,间隔的时间ms);
比如:

setInterval(function(){
	alert("Welcome to my blog!");
},3000);

每3s中输出 Welcome to my blog!

通过setTimeout 也可以实现 setInterval:

function testfoo(){
	alert("You are very handsome!");
	setTimeout(testfoo,1000);
}
setTimeout(testfoo,1000);

回调一下
测试Code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Test</title>
		<script type="text/javascript">
			function init(){
				time = document.getElementById("timer");
				setInterval(conter,1000);
			}
			function conter(){
				time.innerHTML = time.innerHTML - 1;
				if(time.innerHTML < 0){
					document.location = "http://baidu.com";
				}
			}
			
		</script>
	</head>
	<body onload="init()">
		<div id="timer">10</div>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_43635647/article/details/104624329