監聽用戶長時間不操作

在這裏插入圖片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>監聽用戶長時間不操作</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0, minimum-scale=1, maximum-scale=1, viewport-fit=cover"/>
</head>
<body>
	<h1>3秒不點擊將會跳轉到百度</h1>
</body>

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
	let currtentTime = new Date().getTime(),
	     lastTime = new Date().getTime(), 
	     diff = 3000;
	 $(document).on('click',function(){
	 	lastTime = new Date().getTime();
	 });
	 let timer = setInterval(function(){
	 	currentTime = new Date().getTime();
	 	if(currentTime - lastTime > diff){
	 	   $(location).attr('href','http://www.baidu.com');  //跳轉到指定目錄
	 	   clearInterval(timer);
	 	}
	 },1000);
</script>

</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章