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
function Delayer()
{
setTimeout('Redirection()', 5000);
}
function Redirection()
{
window.location="http://www.google.com";
}
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
0 comments:
Post a Comment