Page Redirect After X Seconds Wait Using Javascript
I Need to Redirect to Specific Url After 5 Seconds After Putting an Error Message. First I Have Used Javascript as Below. Document. Ready(Window...
I need to redirect to specific url after 5 seconds after putting an error message. First i have used Javascript as below.
document.ready(window.setTimeout(location.href = "",5000));
But it is not waiting for 5 seconds. Than I searched the issue in google than come to know that "document.ready()" is invoked when document is loaded at DOM, not at web browser.
Than i have used window.load() function of jQuery but still i am not getting what i want.
$(window).load(function() {
window.setTimeout(window.location.href = "",5000);
});
Can anyone please let me know how exactly i need to do to wait for 5 seconds.
It looks you are almost there. Try:
if(error == true){
// Your application has indicated there's an error
window.setTimeout(function(){
// Move to a new location or you can do something else
window.location.href = "";
}, 5000);
}
You actually need to pass a function inside the window.setTimeout() which you want to execute after 5000 milliseconds, like this:
$(document).ready(function () {
// Handler for .ready() called.
window.setTimeout(function () {
location.href = "";
}, 5000);
});
For More info: .setTimeout()
You can use this JavaScript function. Here you can display Redirection message to the user and redirected to the given URL.
<script type="text/javascript">
function Redirect()
{
window.location="";
}
document.write("You will be redirected to a new page in 5 seconds");
setTimeout('Redirect()', 5000);
</script>
You need to pass a function to setTimeout
$(window).load(function () {
window.setTimeout(function () {
window.location.href = "";
}, 5000)
});
Use JavaScript setInterval() method to redirect page after some specified time. The following script will redirect page after 5 seconds.
var count = 5;
setInterval(function(){
count--;
document.getElementById('countDown').innerHTML = count;
if (count == 0) {
window.location = '
}
},1000);
Example script and live demo can be found from here - Redirect page after delay using JavaScript
$(document).ready(function() {
window.setTimeout(function(){window.location.href = ""},5000);
});
setTimeout('Redirect()', 1000);
function Redirect()
{
window.location="";
}
//document.write("You will be Redirect to a new page in 1000 -> 1 Seconds, 2000 -> 2 Seconds");
<script type="text/javascript">
function idleTimer() {
var t;
//window.onload = resetTimer;
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer; // catches mouse clicks
window.onscroll = resetTimer; // catches scrolling
window.onkeypress = resetTimer; //catches keyboard actions
function logout() {
window.location.href = 'logout.php'; //Adapt to actual logout script
}
function reload() {
window.location = self.location.href; //Reloads the current page
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 1800000); // time is in milliseconds (1000 is 1 second)
t= setTimeout(reload, 300000); // time is in milliseconds (1000 is 1 second)
}
}
idleTimer();
</script>