Javascript - 页面重新加载脚本无法正常工作

时间:2018-01-17 20:26:41

标签: javascript redirect timer

我是javascript的完整新手,其中大部分代码都来自其他网站。在用户处于非活动状态达指定的时间后,我尝试使用我发现的两个重定向页面。

我能够让计时器工作并重新加载页面而不是重定向,但我的重定向代码由于某种原因无效。

编辑:忘记提及此代码需要适用于特定页面,因为我将使用一个页面重定向到特定页面,而另一个页面将重定向到另一个页面。



jQuery(document).ready(function( $ ){
  


var idleTime = 0;
$(document).ready(function () {
    //Increment the idle time counter every minute.
    var idleInterval = setInterval(timerIncrement, 10000); // 10 seconds

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });
});
 function timerIncrement() {
    idleTime = idleTime + 1;
     if ((idleTime > 0) && (window.location.pathname == '/wp'))  { // 10 seconds
       window.location.href = "https://www.google.com";
    }
 
}

  });




1 个答案:

答案 0 :(得分:-1)

我试过你的代码,它有效,但错了。如果它不适合您 - 请删除&& (window.location.pathname == '/wp')并重试。你有更大的问题,你的代码只需在10秒后重定向,无论如何。你需要替换类似的东西:



jQuery(document).ready(function( $ ){
  


var idleTime = 0;
$(document).ready(function () {
    //Increment the idle time counter every minute.
    var idleInterval = setInterval(timerIncrement, 1000); // 1 second

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });
});
 function timerIncrement() {
    idleTime = idleTime + 1;
     if ((idleTime > 9) && (window.location.pathname == '/wp'))  { // 10 seconds
       window.location.href = "https://www.google.com";
    }
 
}

  });




相关问题