Loading Please Wait...
JavaScript can be executed in time-intervals. This is called timing events.
The window object allows execution of code at specified time intervals.
These time intervals are called timing events.
The two key methods to use with JavaScript are:
The setTimeout() and setInterval() are both methods of the HTML DOM Window object.
The window.setTimeout() method can be written without the window prefix.
The first parameter is a function to be executed.
The second parameter indicates the number of milliseconds before execution.
<button onclick="setTimeout(myFunction, 3000)">Try it</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
The clearTimeout() method stops the execution of the function specified in setTimeout().
The window.clearTimeout() method can be written without the window prefix. The clearTimeout() method uses the variable returned from setTimeout().
If the function has not already been executed, you can stop the execution by calling the clearTimeout() method.
<button onclick="myVar = setTimeout(myFunction, 3000)">Try it</button>
<button onclick="clearTimeout(myVar)">Stop time</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
The setInterval() method repeats a given function at every given time-interval.
The setInterval() method can be written without the window prefix.
The first parameter is the function to be executed.
The second parameter indicates the length of the time-interval between each execution.
The first parameter is the function to be executed.
setInterval(myTimer, 1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
The clearInterval() method stops the execution of the function specified in setInterval().
The window.clearInterval() method can be written without the window prefix.
The clearInterval() method uses the variable returned from setInterval()
<p id="demo"></p>
<button onclick="clearInterval(myVar)">Stop time</button>
<script>
let myVar = setInterval(myTimer, 1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved