August 4, 2009

0 Javascript Timing Events

With javascript, it is possible to execute some code NOT immediately after function is called but after a specified time interval. This is called timing events.

It's very easy to time events in javascript. The two key methods that are used are:


  • setTimeout( ) - executes a code some time in the future
  • clear Timeout( ) - cancels the setTimeout( )
Note : the setTimeout( ) and clearTimeout() are both methods of the HTML DOM Window object.

Example

When the button is clicked in the example below, an alert box will be displayed after 5 seconds

<html>
<head>
<script type="text/javascript">
function timedMsg( )
{
var t=setTimeout ("alert( ' 5 seconds ! ' ) ", 5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!" onClick="timedMsg( ) ">
</form>
</body>
</html>

0 comments:

Post a Comment

Blogger Themes

 
Powered by Blogger