Easy Tutorial
❮ Android Tutorial Activity Css Calendar Style ❯

JavaScript Timers

Category Programming Techniques

JavaScript timers have the following two methods:


setInterval()

Syntax

setInterval(code, millisec, lang)
Parameter Description
code Required. The function to be called or the code to be executed.
millisec Required. The time interval between executions of code, in milliseconds.
lang Optional. JScript VBScript JavaScript

The following example executes the clock() function every 1000 milliseconds. It also includes a button to stop the execution:

<html>
<body>

<input type="text" id="clock" />
<script type="text/javascript">
var int = self.setInterval("clock()", 1000);
function clock() {
    var d = new Date();
    var t = d.toLocaleTimeString();
    document.getElementById("clock").value = t;
}
</script>

<button onclick="int = window.clearInterval(int)">Stop</button>

</body>
</html>

setTimeout()

Syntax

setTimeout(code, millisec, lang)
Parameter Description
code Required. The function to be called or the JavaScript code to be executed.
millisec Required. The number of milliseconds to wait before executing the code.
lang Optional. Script language can be: JScript VBScript JavaScript

Example demonstration:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>

<p>Click the button to wait 3 seconds and then see a "Hello" alert.</p>
<button onclick="myFunction()">Click me</button>

<script>
function myFunction() {
    setTimeout(function(){ alert("Hello"); }, 3000);
}
</script>

</body>
</html>

** Click to Share Notes

Cancel

-

-

-

❮ Android Tutorial Activity Css Calendar Style ❯