How to perform a function N times in 1 second

Asked

Viewed 62 times

3

I need to perform a function N times, but what I have done so far has not worked, remembering that N times has to be distributed equally by 1 second.

async function sendImage() {
    Console.log('teste');
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
    while(true){
        sendImage();
        await sleep(1000/60);
    }
}

demo();
  • No, it’s pure javascript.

2 answers

1

Why not pass the value "N times" in the function demo(), in this way:

demo(5); // executar função 5 vezes em 1 segundo

And in function demo() receive and divide this value into 1000, which equals 1 second:

async function demo(n) {
    while(true){
        sendImage();
        await sleep(1000/n);
    }
}

Would look like this:

async function sendImage() {
    console.log('teste');
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo(n) {
    while(true){
        sendImage();
        await sleep(1000/n);
    }
}

demo(5); // 5 vezes em 1 segundo

As noted, the correct is console.log (all tiny), but the script to run.

  • As I said, my code does not work, for example, using the code you provided in the answer, I put to do 60 times a second, but as you can see in this image i.stack.Imgur.com/dtLmh.png the time between each console.log is different. As I put 60, it would be 1000/60 = 16.6666.... That is, every 16.6666 milliseconds was supposed to have a.log console, but that doesn’t happen with my code. Just this my problem, I need this to be accurate in milliseconds.

  • @Lucascarezia I get it. I’ll see here...

  • Now I’ve got it. I believe that the accuracy in time will be defined by the resolution of Promise :p in this case a setInterval may be more recommended.

  • @Lucascarezia Working with very low values in the setTimeout is no guarantee of accuracy. I tested the code in 3 different browsers (Chrome, FF and Opera) and each one shows a different processing value, although approximated. What does that mean? What very low interval values (millionths of a second),)

  • @Lucascarezia ... which would be 4*60 = 240... in Chrome processed 226, in FF 227 and in Opera 232... This, not to mention that in each F5 the value sometimes changes... In conclusion, I think that there is no way to process this accurately, will always process an approximate value...

  • @Lucascarezia If the guy has a bad computer, it will process a lot less yet. That’s why my PC is fast, and yet it doesn’t process everything.

  • All right, I’ll open up a new question, telling you my complete problem to see if you can find a better solution than I’m having right now.

  • @Lucascarezia I have created a test page, if you want to test on several browsers you will see what I am talking about: http://dvdteste.hosting desites.ws/processa.php

  • https:/jsperf.com/

  • @Lucascosta Page does not open... is loading and ends with error 500

  • Here tb @dvd, probably the server is having problems (first time seeing this). But at another time test jsperf.com, if you don’t know it, we can test the performance between scripts on it.

  • @Lucascosta Blz... I’ll wait a while and try again

Show 7 more comments

0

To reach 1 second you need to pass the milliseconds, which is equal to 1000.

async function sendImage(i) {
    console.log('teste ' + Math.random());
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
    while(true){
        sendImage();
        await sleep(1000);
    }
}

demo();

PS: There was a mistake in Console.log, the correct is console.log

Browser other questions tagged

You are not signed in. Login or sign up in order to post.