setTimeout() method is called when delay time has finished. it is not executed immediately. If there are two function setImmediate and setTimeout are called in the current event loop cycle then first call setImmediate after that setTimeout.
setTimeout(() => {
console.log('set Timeout Call');
}, 0);
setImmediate(()=>{
console.log("set Immediate Call")
})
Output:- set Immediate Call
set Timeout Call
set Timeout Call
The clearTimeout() method in javascript clears the timeout which has been set by setTimeout() function.
Javascript setTimeout() method – Interview Questions
Q 1: What is setTimeout()?
Ans: Executes the function once after a delay.
Q 2: Delay unit?
Ans: Milliseconds.
Q 3: How to cancel?
Ans: clearTimeout().
Q 4: Is it blocking?
Ans: No.
Q 5: Use case?
Ans: Delayed execution.
Javascript setTimeout() method – Objective Questions (MCQs)
Q1. setTimeout() is used to ______.
Q2. setTimeout() executes the function ______.
Q3. Which method cancels a timeout?
Q4. What is the default unit of delay time?
Q5. setTimeout() returns a ______.