如何更改此代码,以便它只在没有鼠标移动时重定向 [英] How To Alter This Code So That It Only Redirects If There Is No Mouse Movement
问题描述
我有以下java脚本将用户重定向到另一个网站(在本例中为google)。
I have the following java script which redirects the user to another website (google in this case).
<script type="text/JavaScript">
window.setTimeout("location=('http://www.google.com');",5000);
</script>
然而,虽然我希望网站重定向到另一个网站,我不希望它重定向到否原因。我的目标是网站在光标没有移动一段时间的情况下自动重定向。
However, although i want the website to redirect to another website, i dont want it to redirect for no reason. My aim is for the website to redirect automatically on the condition that the cursor hasnt been moved for a little while.
这可能吗?
推荐答案
这样的东西应该可行,虽然在较旧的MSIE上你需要使用相当于的addEventListener()
Something like this should work, although on older MSIE you'd need to use their equivalent of addEventListener()
var timer = null;
function goAway() {
clearTimeout(timer);
timer = setTimeout(function() {
window.location = 'http://www.google.com/';
}, 5000);
}
window.addEventListener('mousemove', goAway, true);
goAway(); // start the first timer off
它所做的只是确保每次移动鼠标时计时器被清除,然后再次启动。
All it does is ensure that every time the mouse is moved the timer is cleared, and then started up again.
演示 http://jsfiddle.net/alnitak/sXwHY/ ,虽然跨框架安全性使其在那里正常工作。
Demo at http://jsfiddle.net/alnitak/sXwHY/, although cross frame security stops it working correctly there.
这篇关于如何更改此代码,以便它只在没有鼠标移动时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!