JavaScript新建窗口定时关闭窗口小案例

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>window对象</title>
		<script type="text/javascript">
			var timer;
			var n=5;
			var handler;
			function openWindow(){
				handler=window.open("","new_window","width=300,height=250");
				timer==setInterval(print,1000);//
			}
			function print(){
				handler.document.body.innerHTML="离窗口关闭还有"+n+"秒钟";
				n--;
				if(n<0){
					//取消定时器
					clearInterval(timer);
					//关闭子窗口
					n=5;
					handler.close();
				}
			}
		</script>
	</head>
	<body>
		<input type="button" value="打开一个新窗口" onclick="openWindow();" />
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/liyunfu233/article/details/83615373