定时执行一次window.setTimeout。
 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
	function showMsg()
	{
		alert("hello");
	}

	setTimeout(showMsg, 1000);
	
</script>

</head>
	
<body>
</body>
</html>

 

周期性执行window.setInterval。
 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
	
	function showMsg()
	{
		alert("hello");
	}
	
	setInterval(showMsg, 1000);

</script>

</head>
	
<body>
</body>
</html>

 

要结束setTimeout的定时器,可以使用window.clearTimeout。

 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
	function showMsg()
	{
		alert("hello");
	}
	
	var timer = setTimeout(showMsg, 1000);
	clearTimeout(timer)
</script>

</head>
	
<body>
</body>
</html>

 

要结束setInterval的定时器,可以使用window.clearInterval。
 

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
	var timer = null;
	
	function showMsg()
	{
		alert("hello");
		
		if(null != timer)
		{
			clearInterval(timer)
		}
	}
	
	timer = setInterval(showMsg, 1000);

	
</script>

</head>
	
<body>
</body>
</html>