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.
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()
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()
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().
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();
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);
}
}
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 ...
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....
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 ...