Settimeout Method: In this method, a function is executed exactly when the specified waiting time is over. This is a method of HTML DOM window object.
Syntax: window.setTimeout(function, milliseconds);
The first parameter is an executable function.
The second parameter is the time in millisecond which indicates that after a certain period of time the request will be processed.
Example:
<button onclick="setTimeout(myFunction, 4000)">Click me</button>
<script>
function myFunction() {
alert('This is Intellipaat');
}
</script>
SetInterval Method: This method repeats a specific function after every given time interval.
Syntax: window.setInterval(function, milliseconds);
First parameter is a function which needs to be executed.
Second parameter is a time interval in milliseconds where the specific function will be executed after a given time.
Code:
var myVar = setInterval(myTimer, 2000);
function myTimer() {
var a = new Date();
document.getElementById("demo").innerHTML = a.toLocaleTimeString();
}
Want to become an expert in web technology, check out the Web Developer Certification courses offered by Intellipaat.