codescracker


javascript

JavaScript setTimeout() Method



« Previous Tutorial Next Tutorial »


The setTimeout() method in JavaScript is used to specify the time interval after which the code executes.

You can use the setTimeout() method to delay the execution of specific code.

JavaScript setTimeout() Method Example

Here is an example demonstrates setTimeout() method in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript setTimeout() Method</title>
	<script type="text/javascript">
		function timemsg()
		{
			var st = setTimeout("alert('2 seconds!')", 2000);
		}
	</script>
</head>
<body>

<h3>JavaScript setTimeout() Method Example</h3>
<p>Click on the button below to show alert box after 2 seconds.</p>
<form>
	<input type="button" value="Timed Alert Box" onClick="timemsg()">
</form>

</body>
</html>

Here are some sample outputs of the above JavaScript setTimeout() method example. This is the initial output:

javascript settimeout method

Now press Timed Alert Box button, and wait for 2 seconds, you will watch an alert box which will come out after 2 seconds. Here is the snapshot after 2 seconds on pressing Timed Alert Box:

javascript settimeout method example

Here is the live demo output of the above setTimeout() method example in JavaScript.

Click on the button below to show alert box after 2 seconds.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test