我已经在 IE 中使用此代码一段时间了,以便在 5 秒没有事件后重新加载页面。我不明白为什么它在 Chrome 中停止工作。
<script language="JavaScript" type="text/javascript">
<!--
if (document.layers) {
window.captureEvent(onmousemove);
}
document.onmousemove = oreset
var tID = '';
function oreset(){
clearTimeout(tID)
count=0
reloadPage()
}
function reloadPage(){
count++
if(count==5){ // 5 seconds
window.location="/sd/clockin/testclockin.php";
}
tID = setTimeout("reloadPage()",1000);
}
//-->
</script>
最佳答案
我无法告诉你为什么这在 Chrome 中不起作用,但这里有一个总体上更好的脚本:
var expirer=setTimeout(expireFunc,5000);
function expireFunc(){
window.location.replace('/sd/clockin/testclockin.php');//or use .href instead if you don't like replace
}
//work around the random chrome bug here
var lastX=0,lastY=0;
window.addEventListener('mousemove',function(e){
//more workaround stuff
if(e.clientX===lastX&&e.clientY===lastY)
return;
lastX=e.clientX;
lastY=e.clientY;
console.log('moved');
clearTimeout(expirer);
expirer=setTimeout(expireFunc,5000);
},false);
我还必须解决我在评论中解释的错误。 http://jsfiddle.net/7cT9U/是一个工作示例。
关于当我从 IE 切换到 Chrome 时,Javascript 不会重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151300/