0

I am attempting to make a simple while (true) {} loop, but it waits 1 second before running the code inside it. Basically, I want a system something like this:

Hello, world! (waits 1 second) Hello, world! (waits 1 second) Hello, world! (waits 1 second) Hello, world! (waits 1 second) Hello, world! (waits 1 second) and so on.

But I keep on receiving this error:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

My code:

while (true) {
    setTimeout(() => {
        console.log(`Hello, world!`)
    }, 1000);
}

Thanks in advance!

Dan3DTV
  • 11
  • 3
  • 1
    What your code does is schedule timer callbacks repeatedly until it runs out of memory; none of the timers actually has a chance to run. Nothing makes the `while` loop wait for the timer callback to occur. See the linked question's answers for more details. – T.J. Crowder Sep 28 '20 at 17:11
  • @Dan3DTV Yes. Exactly, it is an infinite loop that you are creating and hence it is running out of memory. – kavigun Sep 28 '20 at 17:47

0 Answers0