'setInterval() doesn't work if a forever loop exists [duplicate]

Overview: (Skip if you want) I have basic knowledge of Javascript, I usually use javascript using node.js to code discord bots. As discord users know, discord already has bots for almost EVERYTHING. I needed some special ideas so I just decided to play around some javascript features and frameworks, hoping to get some ideas from it.

Problem: I discovered a function called setInterval(function, time), I was testing / playing with this function until I discovered something, here is my code:

console.log(`Starting interval with id: ${setInterval(() => {
    console.log(`I at this time is ${i}`)
}, [500])}`);

let i = 0;
for(;;++i);

I was expecting this to work but sadly it didn't. The variable i successfully gets incremented but the callback of setInterval() is never called.

My theory is that the processor is busy due to the high speed of the for(;;++i) so the callback doesn't get time to execute.

I tried to search about this on google, but I didn't know what to search, I even tried searching it on stack overflow but couldn't find anything.

I dont't know if this is a real question, neither do I know if this has a fix, but could someone at least tell me why this problem is occuring, am I right with my theory above?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source