Redirection in javascript after some time delay

In this programming tutorial we will learn how to redirect the user to another web page or website after some time delay. Its quite easy in javascript as you have to write just single line of code. Lets have a look over the example given below.
Redirection in javascript after some time delay
For this purpose there is a built-in javascript method setTimeout().
setTimeout() method is used to repeatedly call some function after a specified amount of time.

Syntax of setTimeout() Method
setTimeout("Javascript Statement",Milliseconds);

Note: - The setTimeout() is the method of the HTML DOM Window object.

setTimeout() method takes two parameters. The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('2 seconds!')" or a call to a function, like "helloMsg()".

The second parameter indicates how many milliseconds from now you want to execute the first parameter.

Note: - There are 1000 milliseconds in one second.

So lets have a look over the code
<html>
<head>
<title>Redirection in javascript after some time delay</title>
<script language="javascript" type="text/javascript">
function Delayer()
{
setTimeout('Redirection()', 5000);
}
function Redirection()
{
window.location="http://www.google.com";
}
</script>
</head>
<body>
<form>
<input type="button" value="Redirect after 5 secs" onclick="Delayer();" /></form>
</body>
</html>
The above mentioned code will redirect user to the google.com after 5 seconds of clicking the button. When user will click the button, the function Delayer() will be get call and in this function I am calling Redirection() function after every 5 seconds. So when focus will move to the Redirection() function after every 5 seconds then the user will be immediately redirected to the google.com

So that's it.
I love your feedback.

2 comments:

  • Anonymous
     

    It's Very Informative Article.

    Nice Job...!

  • www.digita-book.com
     

    I think this really helps me and everybody.
    Great post
    Thanks
    http://www.adnpost.com
    http://www.digita-book.com