0

I have two functions

  1. console.log() running every second, using setInterval()
  2. a constant loop calculating (eg. repeatedly calculating sha256)

Using asynchronous JavaScript, how can I run both a constant loop and still console.log() every second.

Everything I've tried brought no solution, as the calculating loop always goes to the top of JavaScript's queue and runs until it I manually stop this loop and then the interval starts going every second

I know that a solution also involves Web Workers, but I would rather like to understand this first.

ossy
  • 19
  • 5
  • 2
    In short: The JS engine is too busy running your loop to go and look at the queue of tasks being generated by setInterval. – Quentin Dec 23 '21 at 14:48
  • 2
    TL;DR: Only one thing can happen at a time. If a loop is blocking the thread, then no `console.log` can squeeze in between that. – deceze Dec 23 '21 at 14:48

0 Answers0