+ 3
how to loop numbers from 1 to 20 and after a wait time loop 20 back to 1.
I want to change numbers every after 100 seconds from 1 to 20 then await 4 seconds then loop 20 back to 1.(i tried setTimeout() but i failed any example suggestions are welcome);
12 Answers
+ 4
đ€Web Gigđ€
Is it correct??
https://code.sololearn.com/Wlqq7Vl17x5h/?ref=app
+ 3
I tried to fix the failed code you sent.Hope this helps.
https://code.sololearn.com/WqH8Rf4xQUQG/?ref=app
+ 2
will u please show your failed Code ?
+ 2
đ€Web Gigđ€ something like this ?
let counter=1;
let num=1;
function start(){
let change=setInterval(()=>{
counter+=num;
console.log(counter);
if(counter==20 || counter==1){
clearInterval(change)
setTimeout(()=>{
num=-num;
start()
},4000)
//how to await 4 seconds and loop 20 back to 1
}
},100);
}
start()
+ 1
HrCoder check it out is no good:https://code.sololearn.com/WWNslwhYi4o8/?ref=app
+ 1
let counter=1;
let change = () => {
console.log(counter);
document.body.innerText = counter;
if(counter==20) {
counter=1;
setTimeout(change, 4000); // await 4 seconds
} else {
counter++;
setTimeout(change, 1000); // 1 sec
}
}
change();
+ 1
https://code.sololearn.com/W1fHIKCyWW4g
let timer;
let counter=1;
let counterAsc = () => {
console.log(counter);
document.body.innerText = counter;
if(counter==20) {
clearTimeout(timer);
timer = setTimeout(counterDesc, 4000); // await 4 seconds
} else {
counter++;
timer = setTimeout(counterAsc, 1000); // 1 sec
}
}
let counterDesc = () => {
console.log(counter);
document.body.innerText = counter;
if(counter==1) {
clearTimeout(timer);
timer = setTimeout(counterAsc, 4000); // await 4 seconds
} else {
counter--;
timer = setTimeout(counterDesc, 1000); // 1 sec
}
}
counterAsc();
+ 1
Another alternative using Promise
https://code.sololearn.com/WJxLfITM7E5S
- 1
Waaaa zebiiiiiiii
- 1
Fack youuuuuu
- 1
Hyy