As per MDN, the callbacks in setTimeout()
function are executed only when the stack on the main thread is empty.Is this same for setInterval()
function? If yes then when both these function are present then which will be executed first?
I am using node.js environment I am getting some inconsistent result with below code
var i = 0;
function cal(){
i++;
console.log(i);
if(i == 2) clearInterval(inter);
}
const inter = setInterval(cal,2000);
setTimeout(()=> {
console.log('Hello');
},2000)
the output I am getting is
1
Hello
2
but If I run below code
setTimeout(()=> {
console.log('Hello');
},2000)
var i = 0;
function cal(){
i++;
console.log(i);
if(i == 2) clearInterval(inter);
}
const inter = setInterval(cal,2000);
the output I am getting is
Hello
1
2