Introduction:
In this article you will learn how the delay time is being set in JavaScript. Timeouts are like a timer. It can allow you to execute a section of code after a specified amount of time has passed. If any user wants to display the output after some time then you will set time as follows:
Syntax:
setTimeout("function expression",delaytime)
The delaytime is entered as milliseconds (1 second=1000 millisecond).
Example: In this example you will see how to display an alert box automatically after some time.
<html>
<head>
<title>Time in alert box</title>
</head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('Welcome to dotnetheaven.com')",4000)
}
</script>
<body>
<input type="button" value="Display Timed alertbox!" onClick = "timedMsg()">
<form>
<p>
<font color="maroon">
Click on the button. An alert box will be displayed after 4 seconds.
</font>
</p>
</form>
</body>
</html>
Once the button is activated, the OnClick event goes to the function called "timedMsg". The function creates an ALERT box to appear after 4 seconds.
Output: Output of this script is as follows:
Figure 1: Output of the above script.
When you click on "Display timed alertbox" button then alert box will open after four second (see figure 2).
Figure 2: Display alert box after four second.
In this example I have set delaytime for four second (4000 milliseconds). So the alert box will display after four second.