[英]jQuery - Redirect webpage after 5 seconds
如果我在那里停留一段時間,我想將我的頁面重定向到另一個頁面。 我嘗試編寫以下腳本並將其放在我的網頁的頭部,但它不起作用。 移動的位置不是真正的網址,因為我在 XAMPP 上。
$(document).ready(setTimeout(function () {
window.location.replace("../index.php");
}, 5000););
您給出的方式完全錯誤,導致語法錯誤。 檢查您的控制台。 ready()
函數需要一個函數而不是一個整數(由setTimeout()
返回)。
試試這個方法:
$(function () {
setTimeout(function() {
window.location.replace("../index.php");
}, 5000);
});
或者,如果您只想在 5 秒不活動后使用,則需要通過檢查用戶活動( keypress
、 mousemove
)來使用不同的方法,然后清除計時器並重新啟動它。
如果您想在 5 秒不活動后嘗試重定向,您可以這樣做:
var timer = 0;
function startRedirect() {
timer = setTimeout(function () {
window.location.replace("../index.php");
}, 5000);
}
function restartTimer() {
clearTimeout(timer);
startRedirect();
}
$(function () {
startRedirect();
$(document).mousemove(restartTimer).keyup(restartTimer);
});
您可以在沒有 JS 的情況下通過將正確的元標記放入標題中來做到這一點
<head>
<meta http-equiv="Refresh" content="5; url=http://google.com">
</head>
其中“5”是等待超時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.