[英]Javascript - page reload script won't work
我是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"; } } });
我試過了您的代碼,它可以工作,但是錯了。 如果對您不起作用,請刪除該&& (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"; } } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.