+ 1

How to keep box animating forever?

So I'm trying to animate a box. I got till how to animate it to the right but it stops there. I want it to now go to the left and then to the right and loop like that. What should I add to my code for it to do that? Here's my existing code : var pos = 0; var box = document.getElementById("box"); var t = setInterval (move, 10); function move () { if (pos >= 150) { clearInterval(t); } else { pos += 1; box.style.left = pos+"px"; _____________ Thank you.

15th Nov 2020, 8:36 AM
Aravind-R-C 06
Aravind-R-C 06 - avatar
5 Respuestas
+ 1
AravindR2006 instead of writing clear interval in if try to write pos = 0;
15th Nov 2020, 10:54 AM
Krish
Krish - avatar
15th Nov 2020, 10:50 AM
Aravind-R-C 06
Aravind-R-C 06 - avatar
+ 2
That works, thank you Krish !
15th Nov 2020, 10:55 AM
Aravind-R-C 06
Aravind-R-C 06 - avatar
0
Please share your code file for more specifications
15th Nov 2020, 10:48 AM
Krish
Krish - avatar
0
AravindR2006 thanks 😊😊
15th Nov 2020, 10:56 AM
Krish
Krish - avatar