+ 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);

5th Sep 2021, 10:53 AM
đŸ€–Web GigđŸ€–
đŸ€–Web GigđŸ€– - avatar
12 RĂ©ponses
6th Sep 2021, 2:44 AM
☆~Monu~☆
☆~Monu~☆ - avatar
+ 3
I tried to fix the failed code you sent.Hope this helps. https://code.sololearn.com/WqH8Rf4xQUQG/?ref=app
6th Sep 2021, 9:35 AM
A P
A P - avatar
+ 2
will u please show your failed Code ?
5th Sep 2021, 10:56 AM
Pariket Thakur
Pariket Thakur - avatar
+ 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()
5th Sep 2021, 12:22 PM
Abhay
Abhay - avatar
5th Sep 2021, 11:03 AM
đŸ€–Web GigđŸ€–
đŸ€–Web GigđŸ€– - avatar
+ 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();
5th Sep 2021, 12:26 PM
SoloProg
SoloProg - avatar
5th Sep 2021, 12:43 PM
Pariket Thakur
Pariket Thakur - avatar
+ 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();
5th Sep 2021, 12:57 PM
SoloProg
SoloProg - avatar
+ 1
Another alternative using Promise https://code.sololearn.com/WJxLfITM7E5S
5th Sep 2021, 8:14 PM
ODLNT
ODLNT - avatar
- 1
Waaaa zebiiiiiiii
6th Sep 2021, 4:36 PM
Rida Boukouja
- 1
Fack youuuuuu
6th Sep 2021, 4:36 PM
Rida Boukouja
- 1
Hyy
6th Sep 2021, 4:37 PM
Rida Boukouja