JS浏览器对象 --计时器

1.计时方法

1)setInterval() -间隔指定的毫秒数不停地执行指定的代码

     clearInterval() -停止setInterval()方法执行的函数代码

2)setTimeout() -暂停指定的毫秒数后执行指定的代码   (递归)

     clearTimeOut()

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
</head> 
<body>
	<button onclick="stopTime()">an</button>
	<p id="pid"></p>
	<script type="text/javascript">
		var mytime = setInterval(function(){
			getTime();
		},1000);
		function getTime(){
			var d = new Date();
			var t = d.toLocaleString();
			document.getElementById("pid").innerHTML = t;
		}
		function stopTime(){
			clearInterval(mytime);
		}
	</script>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
</head> 
<body onload="myWin()">
	<button onclick="stopWin()">an</button>
	<p id="pid"></p>
	<script type="text/javascript">
		var win;
		function myWin(){
			alert("hello");
			win = setTimeout(function(){myWin()},3000);
		}
		function stopWin(){
			clearTimeout(win);
		}
	</script>
</body>
</html>




猜你喜欢

转载自blog.csdn.net/tree_building/article/details/79558032