Implementing a timed delay in JavaScript is useful in the following situations:
- Showing an “Update your bookmark” page when you have to change URLs or page locations
- For download sites that wish to have a timed delay before the download starts
- To refresh a webpage once every specified number of seconds
The code for this timed delay is slightly involved and is beyond the scope of this tutorial. However, we have tested it and it seems to function properly.
<html> <head> <script type="text/javascript"> <!-- function delayer(){ window.location = "../javascriptredirect.php" } //--> </script> </head> <body onLoad="setTimeout('delayer()', 5000)"> <h2>Prepare to be redirected!</h2> <p>This page is a time delay redirect, please update your bookmarks to our new location!</p> </body> </html>
The most important part of getting the delay to work is being sure to use the JavaScript functionsetTimeout. We want the delayer() function to be used after 5 seconds or 5000 milliseconds (5 seconds), so we pass the setTimeout() two arguments.
- ‘delayer()’ – The function we want setTimeout() to execute after the specified delay.
- 5000 – the number of millisecods we want setTimeout() to wait before executing our function. 1000 miliseconds = 1 second.
0 comments:
Post a Comment