JavaScript 延迟执行,定时器的学习 clearTimeout ,setTimeout

时间:2021-02-02 00:08:56
<!DOCTYPE html>
<html>
<head>
	<title>倒计时</title>
	<meta charset="utf-8">
</head>

<script type="text/javascript">
var c = 0;
var t;
	function onChange(){
		document.getElementById('myText').value = c;
		c += 1;
		t = setTimeout("onChange()", 1000);
	}
	function onStop () {
		c = 0;
		clearTimeout(t);
		document.getElementById("myText").value = c;
	}
</script>

<body>
	<input type="button" value="点击按钮,进行倒计时" onclick="onChange()"> 
	<input type="text" id="myText">	
	<input type="button" value="点击按钮,停止倒计时" onclick="onStop()"> 

	</br>
	<p>点击按钮,将从0 开始进行循环 + 1</p>
</body>
</html>