0

Using a for with big numbers make the browser freeze or even crash. Is there a way to run these loops and not freezing the page?

Using Promisses, async functions maybe? How should i do that?

Exemple:

for(i=0; i<1000000000; i++){}

Im developing a MIDI player in a website, i need the precision that comes from the for or the forEach instead of adding one millesecond using setInterval.

var renderTrack = (track, play) => {
      track.notes.forEach(note=>{
        if(play){
          notesArr.push(setTimeout(()=>{
            pianoPress(note.name + note.octave, note.velocity);
          }))
        }
      })
    }

    MIDI.tracks.forEach(track=>{
      renderTrack(track, true);
    });
Luís HNrique
  • 216
  • 1
  • 14
  • 1
    And/or use `timeout` or `interval` to break task into chunks? – iAmOren Sep 03 '20 at 08:53
  • 3
    Does this answer your question? [How to process the big loop without freezing the browser using setTimeOut function in javascript?](https://stackoverflow.com/questions/38735201/how-to-process-the-big-loop-without-freezing-the-browser-using-settimeout-functi) – Alexandre Elshobokshy Sep 03 '20 at 08:55
  • 1
    Another interesting post (on node but it applies also in browser): https://snyk.io/blog/nodejs-how-even-quick-async-functions-can-block-the-event-loop-starve-io/ – Mosh Feu Sep 03 '20 at 08:58
  • 2
    https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers – Njuguna Mureithi Sep 03 '20 at 09:05
  • Im makeing an midi player, so i need precision in the songs, this 1 milissecond in each note can cause wierd timing in long pieces. I was searching for something that doesnt take out this precision from the for. – Luís HNrique Sep 03 '20 at 09:30

0 Answers0