简体   繁体   中英

Javascript - page reload script won't work

I'm a complete newb to javascript, and much of this code was pulled from other sites. I'm trying to use two things I found to make a page redirect after the user is inactive for a specified amount of time.

I was able to get the timer working and make the page reload instead of redirecting, but my redirect code doesn't work for some reason.

EDIT: forgot to mention this code needs to work for specific pages, as I will be using one page to redirect to a specific page, and another to a different page.

 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"; } } }); 

I tried you code, it works, but wrong. If it don't work for you - remove that && (window.location.pathname == '/wp') and try again. You have bigger problem, your code just redirects after 10 seconds no matter what. You need to replace to something like that:

 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"; } } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM