Friday 2 March 2012

setTimeout() Method Javascript

Definition:
setTimeout() - executes a code some time in the future

Syntax:
var tTime = setTimeout("javascript statement",milliseconds);

The first parameter of setTimeout() can be a string of executable code, or a call to a function.
The second parameter indicates how many milliseconds from now you want to execute the first parameter.

1000 milliseconds = one second

Example:

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

 <html>
<head>
<script type="text/javascript">
function timeMessage()
{
var tTime = setTimeout("HelloWorld()",2000);
}
function HelloWorld()
{
         alert("Hello World");
}
</script>
</head>

<body>
<form>
<input type="button" value="Display alert box in 2 seconds" onclick="timeMessage()" />
</form>
</body>
</html>

No comments:

Post a Comment