The difference between setInterval() method and setTimeout() method is detailed in HTML

setInterval()

Usage: setInterval("method name", time);
Function: execute the method every once in a while.
Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		
		<input type="button" value="查询" onclick="jump()" />
		<input type="button" value="停止" onclick="c()" />
		<input type="button" value="开始" onclick="s()" />
		
		<!--window-->
		<script>
			
			function t(){
				console.log(857);
			}
			var id = setInterval("t()",1000);
			
			function s(){
				id = setInterval("t()",1000);
			}

			//停止setInterval
			function c(){
				clearInterval(id);
			}
			
		</script>
		
		
	</body>
</html>
 

Result display:

Click the end button to stop executing the method, and the number of times is determined to be 4; click the start button, and continue to execute the method once per second from 4 on.

setTimeout()

Usage: setTimeout("method name", time);
Function: After a period of time, the execution of the method ends, that is, the method is executed only once.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		
		<input type="button" value="查询" onclick="jump()" />
		<input type="button" value="停止" onclick="c()" />
		<input type="button" value="开始" onclick="s()" />
		
		<!--window-->
		<script>
			
			function t(){
				console.log(857);
			}
			
			//运算倒计时
			function s(){
				var id = setTimeout("t()",1000);
			}

			//停止setTimeout
			function c(){
				clearTimeout(id);
			}
			
			

		</script>
		
		
	</body>
</html>

Result display:

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/107427112