>
 



Time delay in url redirection may be useful for the following conditions:

  • Refresh a web page in every specified seconds.
  • For displaying an "Update you Bookmark" page when a page Url has ben changed.


The html and JavaScript code for url redirection with delay is given below:

<html>
<head>
<script type="text/javascript">
function delayedRedirect(){
    window.location = "/default.aspx"
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html>

More...

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



JavaScript features a couple of methods that lets you run a piece of JavaScript code (javascript function) at some point in the future. These methods are:

  • setTimeout()
  • setInterval()


In this tutorial, I'll explain how setTimetout() method works, and give a real world example. You may find the details of setInterval() method in JavaScript setInterval Function - JavaScript Timing Events

setTimeout()

window.setTimeout() method allows you to specify a piece of JavaScript code (expression) will be run after specified number of miliseconds from when the setTimeout() method is called.

Syntax

var t = setTimeout ( expression, timeout );

The setTimeout() method returns a numeric timeout ID which can be used to refer the timeout to use with clearTimeout method. The first parameter (expression) of setTimeout() is a string containing a javascript statement. The statement could be a call to a JavaScript function like "delayedAlert();" or a  statement like "alert('This alert is delayed.');". The second parameter (timeout), indicates the number of miliseconds to pass before executing the expression.

Example 

<html>
   <head>
      <script type="text/javascript">
        function delayedAlert()
        {
           var t=setTimeout("alert('You pressed the button 5 seconds ago!')",5000)
        }
      </script>
   </head>
   <body>
      <form>
         <input type="button" value="Display Delayed Alert"
                onClick="delayedAlert();">
      </form>
   </body>
</html>

An alert box will be shown 5 seconds later when you clicked the button.

clearTimeout()

Sometimes it's useful to be able to cancel a timer before it goes off. The clearTimeout() method lets us do exactly that. Its syntax is:

clearTimeout ( timeoutId );

where timeoutId is the ID of the timeout as returned from the setTimeout() method call.

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



Time delay in url redirection may be useful for the following conditions:

  • Refresh a web page in every specified seconds.
  • For displaying an "Update you Bookmark" page when a page Url has ben changed.


The html and JavaScript code for url redirection with delay is given below:

<html>
<head>
<script type="text/javascript">
function delayedRedirect(){
    window.location = "/default.aspx"
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html>

The JavaScript function setTimeout is the most important part of the code given above. setTimeout function calls delayedRedirect function after 3 seconds (3000 miliseconds).

You may find the details of setTimeout() method in JavaScript setTimeout Function - JavaScript Timing Events

Currently rated 4.3 by 4 people

  • Currently 4.25/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Free CMS