js監聽鼠標鍵盤動作,超時不動作觸發相應動作

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

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