JavaScript how to make setTimeout() work as it looks like in for-loop?

how to make setTimeout() work as it looks like in for-loop? like this code

function hello() {
    for (let index = 0; index < 3; index++) {
        setTimeout(function () {
            console.log('What\'s up!')
        }, 3000)
        console.log('Yo')
    }
}

hello()

it logs

Yo
Yo
Yo
What's up!
What's up!
What's up!

How can I make it logs

Yo
What's up(after 3 sec)
Yo
What's up(after 3 sec)
Yo
What's up(after 3 sec)

Thanks for your help.

Answer:1

One way to do it is this:

function hello() {
    for (let index = 1; index <= 3; index++) {
        setTimeout(function () {
            console.log('What\'s up!')
        }, 3000 * index)
        setTimeout(function () {
            console.log('Yo')
        }, 3000 * (index - 1))
    }
}
hello()
Answer:2

You need either Promise or recursion for operations like this.

Promise (without async/await)

async function hello(index = 0) {
    if (index >= 3) return;
    
    index += 1;

    return new Promise(function(resolve){
    
      setTimeout(function(){
        console.log('What\'s up!');
        resolve();
      }, 3000);
      
      console.log('Yo');
      
    }).then(hello.bind(null, index));
}

hello()
Answer:3

setTimeout() will not give you desired result even if you put 0 waiting time. Either put both the console.log in setTimeout() or remove setTimeout().

Answer:4

You need something like this:

const times = 3;
var n = 0;

function logYo(){
  console.log('Yo');
  logWhatsUp();
}

function logWhatsUp(){
  setTimeout(() => {
    console.log('Whats Up');
    n++;
    if(n < times)
      logYo();
  }, 3000);
}

logYo();
Answer:5

You can just make a function which executes itself with setTimeout() but you would have to incorporate a global function as a counter.

let counter = 0;
function hello(n){
    console.log("Yo");
    console.log("What's up?);
    counter++;
    if(counter > n){
        setTimeout(hello, 3000);
    }
}
Answer:6

Promise.prototype.finally has been part of the ECMAScript specification for more than a year and gives no errors in most browsers. When used within Teams desktop app, I see "Promise.prototype....

Promise.prototype.finally has been part of the ECMAScript specification for more than a year and gives no errors in most browsers. When used within Teams desktop app, I see "Promise.prototype....

I'd like my countdown timer to show double digits, like "53:00" instead of "53:0". Not sure what's going wrong here? Thank you! function convertSeconds(s) { var min = Math.floor(s / 60); var ...

I'd like my countdown timer to show double digits, like "53:00" instead of "53:0". Not sure what's going wrong here? Thank you! function convertSeconds(s) { var min = Math.floor(s / 60); var ...

  1. numberformat javascript locale
  2. number format javascript money
  3. number format javascript 2 decimal places
  4. number format javascript commas
  5. number format javascript decimal
  6. number format javascript currency
  7. number format javascript library
  8. number format javascript jquery
  9. number format javascript leading zero
  10. number format javascript php
  11. number format javascript example
  12. number format javascript function
  13. number format javascript input
  14. number format javascript regex
  15. number format javascript decimal places
  16. number_format javascript like php
  17. number format javascript pdf
  18. javascript numberformat comma
  19. javascript numberformat currency
  20. javascript numberformat style

I'm trying to create an admin template with a sidebar that can be toggled. I want it to just slide in and out and the content resize accordingly. I've tried everything I can think of and nothing works....

I'm trying to create an admin template with a sidebar that can be toggled. I want it to just slide in and out and the content resize accordingly. I've tried everything I can think of and nothing works....

  1. semantic ui collapsible sidebar

I have a requirement to allow the user to download the multiple images by checking the file and clicking the button. For now I have provided a link below every image to download. But I need to do ...

I have a requirement to allow the user to download the multiple images by checking the file and clicking the button. For now I have provided a link below every image to download. But I need to do ...

  1. make multiple image meme
  2. make multiple bootable image file
  3. how to make multiple image to pdf
  4. how to make multiple image in photoshop
  5. how to make multiple image slider
  6. make one image from multiple images
  7. image make multiple layers