Hi, I need to create a infinite (but breakable) cycle where I can slow down the cycle by awaiting promises inside. While cycle should be able to do this, but as you can see in the image, the duration is all over the place.

Why is this happening? Is there a way to make it close to the original sleep duration?

  • @JaddedFauceet
    link
    English
    51 year ago

    Here is the code for anyone else who want to try

    async function slowLoop() {
        while (true) {
            console.time('sleepDuration')
            await new Promise(resolve => setTimeout(resolve, 1000))
            console.timeEnd('sleepDuration')
        }
    }
    

    It is not suppose to deviate too much. setTimeout will execute whenever possible at / after the given timeout duration. I am getting between 1006 to 1023 on my sysyem

    Is your device performance starved during this test?

    • iFarmGolemsOP
      link
      English
      1
      edit-2
      1 year ago

      It’s not. I am running process lasso tough but doubt that’s the issue.

      I have Ryzen 5800x3D and tried the code on latest Ms edge (chromium) browser.

      Which browser did you try it on? Or did you try to run it in node?

      The browser tab was active and focused also.

      • @JaddedFauceet
        link
        English
        21 year ago

        I have tested the code in both Chrome and MS Edge.

        Is anything else running on the same page that you are testing?

        You can test this in an empty environment like this: Enter this in your URL, “about:blank” and test the code in the console.

        • iFarmGolemsOP
          link
          English
          11 year ago

          I tried it in about:blank but it does the same. Maybe there is some extension in my browser that slows it down (somehow). Im running tampermonkey but it’s not active on any pages I tried this on.

          I ran the script in nodejs and everything works perfectly, the sleep time is very close to the original value so it’s definitely something inside browser that causes it to lag.