0

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?

  • 1
    An infinite loop just blocks the entire thread. No event will be processed until the code finishes running and it never finishes running. – VLAZ Mar 25 '22 at 13:42
  • In a general manner, don't block your code. You shouldn't have infinite loops or loops that takes a long time to execute. Read about how javascript works – aymcg31 Mar 25 '22 at 13:43

0 Answers0