setInterval() method is used to repeat the execution of your code block at specified intervals.
setInterval(callback, milisecond);
basically two parameters need to run this function.
var number=1;
function incrementNumber(number){
console.log(number)
}
setInterval(()=>{
incrementNumber(number++);
},1000)
Output:- 1
2
3
2
3
Note:-It will run continuously while you close this.
Javascript setInterval() method – Interview Questions
Q 1: What is setInterval()?
Ans: Executes function repeatedly.
Q 2: Time unit?
Ans: Milliseconds.
Q 3: How to stop it?
Ans: clearInterval().
Q 4: Is it asynchronous?
Ans: Yes.
Q 5: Use case?
Ans: Timers and clocks.
Javascript setInterval() method – Objective Questions (MCQs)
Q1. setInterval() is used to ______.
Q2. Which parameter specifies the time delay in setInterval()?
Q3. The time delay in setInterval() is measured in ______.
Q4. Which method is used to stop setInterval()?
Q5. setInterval() returns a ______.