JavaScript - What is setTimeout and setInterval?
setTimeout
is used to execute some code after a
specified amount of time. The function defined in setTimeout is queued
in event loop after the specified interval. The execution would happen
after the timeout, but might not execute immediately after timeout.
setTimeout(() => {
console.log('Executes after timeout');
, 1000);
}//above code executes after 1000ms
setTimeout
with 0ms timeout.
//Making timeout 0 does not make it synchronous
setTimeout(() => {
console.log('Executes after 0 ms');
, 0);
}console.log('Execute on main thread.');
//above prints
//Execute on main thread.
//Executes after 0 ms
setInterval
is used to execute some code repeatedly at
an interval.
setInterval(function(){
console.log('I am printed at an interval of 1 sec.');
, 1000); }
Comments
Post a Comment