Today, we will discuss the difference between setTimeout and setInterval functions, because my friend used these functions in his project and asked me about these functions and their purpose, so I decided to write the basics.
setTimeout():-
setTimout() is a function that takes two parameters.
- Javascript code/statement to execute
- Time in milliseconds
When we use setTimout() the JavaScript code or statement (which we specified as the first parameter) runs only once after the specified time (mentioned as the second parameter).
Code Example:-
function Hello(){
alert("Hi, How are you?????");
}
<input type="button" id="btnSubmit" value="Click Me!!!" onclick="setTimout('Hello()',500)" />
In this example, clicking the button will call Hello() function after 5 milliseconds.
setInterval():-
setInterval() is a function that takes two parameters as well.
- Javascript code/statement to execute
- Time in milliseconds
The difference between the two lies here that when we use setInterval() the JavaScript code(which we specified as the first parameter) executes repeatedly after the specified time interval.
Code Example:-
function Hello(){
alert("Hi, How are you?????");
}
<input type="button" id="btnSubmit" value="Click Me!!!" onclick="setInterval('Hello()',500)" />
In this example, we use setInterval() so you will see that Hello() function will call repeatedly after every 5 milliseconds.
No comments:
Post a Comment
Please do not enter spam links.