-1

i create a function that move element by 1 px and repeat it with a specefic time with setInterval heres The Code :

var Elementt = document.querySelector('.h');
var left = 0;
var speed = 100;
var limit = 10;

setInterval(function MoveByOnepixel() {
    if (left > limit ){

    } else {
        Elementt.style.left = left + 'px';
        left++;
    }
 }, speed);

The thing is when Left > limit is the setinterval stops or going to repeat without stop ?

If it repeat how do i stop it when the if statment is true ?

D. Seah
  • 4,472
  • 1
  • 12
  • 20
user11678472
  • 45
  • 1
  • 5

1 Answers1

0

you can use setTimeout instead

function MoveByOnepixel() {
    Elementt.style.left = left + 'px';
    left++;

    if (left < limit) {
        setTimeout(MoveByOnepixel, speed);
    }
}

setTimeout(MoveByOnepixel, speed);
D. Seah
  • 4,472
  • 1
  • 12
  • 20