如果我在那里停留一段时间,我想将我的页面重定向到另一个页面。我尝试编写以下脚本并将其放在网页的头部,但它不起作用。移动的位置不是真实的 url,因为我在 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);
});
关于javascript - jQuery - 5 秒后重定向网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51019047/