欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js监听鼠标键盘动作,超时不动作触发相应动作

程序员文章站 2022-06-03 11:50:50
...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var state="idle";
var saver ;
function initScreenSaver() {
	// blort;
	saver = new ScreenSaver({
		timeout : 5000
	});
};
function ScreenSaver(settings) {
	this.settings = settings;

	this.nTimeout = this.settings.timeout;

	document.body.screenSaver = this;
	document.body.onmousemove = ScreenSaver.prototype.onevent;     
    document.body.onmousedown = ScreenSaver.prototype.onevent;     
    document.body.onkeydown = ScreenSaver.prototype.onevent;     
    document.body.onkeypress = ScreenSaver.prototype.onevent;    

	var pThis = this;
	var f = function() {
		pThis.timeout();
	};
	this.timerID = window.setTimeout(f, this.nTimeout);
}


ScreenSaver.prototype.timeout = function() {
	//一旦超时,进行处理
	if (!this.saver) {
		if(state=="idle")
		   {	state="busy"; 
		     //  window.location = "http://www.baidu.com" ;
		   }
		
	}
};


ScreenSaver.prototype.signal = function() {
	//未超时时,进行处理
	if (this.saver) {
		this.saver.stop();
	
	}
	if(state=="busy"){
		state="idle";
		}
	//清除超时
	window.clearTimeout(this.timerID);

	var pThis = this;
	var f = function() {
		pThis.timeout();
	};
	this.timerID = window.setTimeout(f, this.nTimeout);
};

ScreenSaver.prototype.onevent = function(e) {
	this.screenSaver.signal();
};

function showstate(){
	setInterval(function(){document.getElementById('state').innerHTML="当前状态:"+state;},100);
	}
	
	
window.onload=function(){showstate();initScreenSaver();}
</script>
</head>

<body>
<div id="state">当前状态:</div>
</body>
</html>