I want to redirect my page to another one if I stay there for a certain time. I tried to write the following script and I put it in the head of my webpage but it doesn't work. The location where to move isn't a real url because I'm on XAMPP.
$(document).ready(setTimeout(function () {
window.location.replace("../index.php");
}, 5000););
The way you have given is totally wrong, causes a Syntax Error. Check your console. The ready()
function expects a function and not an integer (as returned by setTimeout()
).
Try this way:
$(function () {
setTimeout(function() {
window.location.replace("../index.php");
}, 5000);
});
Or if you want to use only after 5 seconds of inactivity, you need to use a different approach by checking the user activity ( keypress
, mousemove
) and then clear the timer and restart it.
If you wanna try the redirect after 5 seconds of inactivity, you can do this:
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);
});
You can do it without JS by putting right meta tag into your header
<head>
<meta http-equiv="Refresh" content="5; url=http://google.com">
</head>
where "5" is wait timeout.
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.