【问题标题】:How to change a page location after inactivity of 5 mins如何在 5 分钟不活动后更改页面位置
【发布时间】:2012-09-06 12:08:28
【问题描述】:

我希望如果我的页面在 5 分钟内没有任何活动,那么页面将被重定向到预定义的页面。

谁能建议我如何使用 JavaScript 或 jQuery 来做到这一点。

【问题讨论】:

  • 一个建议:睡一觉(JS 导管中的timeout)然后重定向……

标签: javascript jquery redirect


【解决方案1】:

如果您想要一个真正的不活动控件,请使用这个库(它也可以控制 鼠标键盘 活动)

jQuery 空闲计时器

http://paulirish.com/2009/jquery-idletimer-plugin/

这是一个使用它的示例:

// idleTimer() takes an optional argument that defines the idle timeout
// timeout is in milliseconds; defaults to 30000
$.idleTimer(10000);


$(document).bind("idle.idleTimer", function(){
 // function you want to fire when the user goes idle
});


$(document).bind("active.idleTimer", function(){
 // function you want to fire when the user becomes active again
});

// pass the string 'destroy' to stop the timer
$.idleTimer('destroy');

【讨论】:

    【解决方案2】:

    可能有类似的东西?

    <body onmousemove="canceltimer()" onclick="canceltimer()">
    
    <script type="text/javascript">
    
    var tim = 0;
    function reload() {
    tim = setTimeout("location.reload(true);",300000);   // 5 minutes
    }
    
    function canceltimer() {
    window.clearTimeout(tim);  // cancel the timer on each mousemove/click
    reload();  // and restart it
    }
    
    </script>
    

    【讨论】:

    • 为什么你们总是在setTimeout 中使用字符串? :(
    • keyup 或类似的东西也需要(我只能使用键盘)
    【解决方案3】:

    您好,请查看这篇关于检测网站中的空闲用户的博客文章。这是一个jQuery Idletimer Plugin。你可以找到demo page here

    简单用法:

    // idleTimer() takes an optional argument that defines the idle timeout
    // timeout is in milliseconds; defaults to 30000
    $.idleTimer(10000);
    

    您从github 获得jQuery Idletimer Plugin 的源代码

    【讨论】:

      【解决方案4】:

      您不能将 META Refresh 设置为您希望超时的秒数并指向相应的重定向页面吗?

      <META HTTP-EQUIV="Refresh" CONTENT="60;url=http://www.mydomain.com/redirect_page.html">
      

      我知道这通常不受欢迎,但它避免了对 jQuery(或 Javascript,就此而言)的需求,并且所有浏览器都支持它。

      有时简单更好,即使它很丑。

      【讨论】:

      • 这根本不起作用。最初的问题要求进行空闲检测。在这种情况下,无论用户是否空闲,它都会刷新。
      猜你喜欢
      • 2015-05-16
      • 1970-01-01
      • 2021-02-06
      • 2020-05-28
      • 2018-06-24
      • 2017-04-09
      • 2020-01-30
      • 2017-11-11
      • 1970-01-01
      相关资源
      最近更新 更多