setInterval()メソッドとsetTimeout()メソッドの違いはHTMLで詳しく説明されています

setInterval()

使用法:setInterval( "method name"、time);
機能:時々メソッドを実行します。
コード例:

<!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>
 

結果表示:

終了ボタンをクリックしてメソッドの実行を停止すると、回数は4回と決定されます。開始ボタンをクリックして、4から1秒に1回メソッドを実行し続けます。

setTimeout()

使用法:setTimeout( "method name"、time);
機能:一定期間が経過すると、メソッドの実行が終了します。つまり、メソッドは1回だけ実行されます。

<!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>

結果表示:

おすすめ

転載: blog.csdn.net/m0_46383618/article/details/107427112