Is there an asynchronous function - does not block fRender thread from continuing?

//------------------------------
glDegrees = 0;
for (let ii = 0; ii < 10000; ii++) {
  glDegrees = ii;
  let lCarCenterX = glCarTopLeftX + gcCarImageWidth / 2;
  let lCarCenterY = glCarTopLeftY + gcCarImageHeight / 2;  
  fRender(lCarCenterX, lCarCenterY, glDegrees);
  fSleep(1000);
} 
//------------------------------
//------------------------------
function fSleep(num) {
  let now = new Date();
  let stop = now.getTime() + num;
  while(true) {
      now = new Date();
      if(now.getTime() > stop) return;
  }
}//fSleep
//------------------------------

The fSleep function blocks the fRender function thread from continuing. Is there an asynchronous function that does not block the fRender thread from continuing?

1 Like

You should use setInterval() or setTimeout()

That’s kind of the point of a sleep function - to ensure smooth behavior and allow enough time for something to respond. If you don’t want it to actually sleep, just take it out…

Thallius, I’ve tried them. They stop the fRender thread.

Delay functions keep the code busy, not allowing anything else to run. I think that I need to use asynchronous code instead.

DaveMaxwell, The fRender needs time to complete it’s task. The fSleep function, which delays properly, consumes CPU time => the fRender thread is stopped, and never changes the display until the very end.

End result: The car jumps from a horizontal position to the end angle position. The angles in-between are never displayed.

This is not because the frender() is stopped, but because your frender() is blocking the JavaScript to output the results

So where did frender() come from? Is it an API? I cannot find any result when I search for frender() in google

Thallius, See Image is not displaying sequentially with a delay function. I had not thought of async as an option when I posted that.