Pergunta

I am using the following script to display jpg images as an animation:

var interval;

function Play() {
  setInterval(Playimages, 100);
}

function Stop() {
  alert("stop");

  clearInterval(interval);
}

function Playimages() {
  i = (i < sl - 1) ? (i + 1) : 0;

  $('#Image1').attr('src', images[i].src);
}

Play works well, however I can not stop or pause; there is a mistake in my clearInterval. Would appreciate your suggestions. Thanks

Foi útil?

Solução

You never assign the interval id to interval. Should be

interval = setInterval(Playimages, 100);

Outras dicas

You need to change

 setInterval(Playimages, 100);

to

interval = setInterval(Playimages, 100);

setInterval returns an id which can be used with clearInterval. In your code interval is probably undefined, so clearInterval does nothing because it has no reference.

You need to assign the interval id to interval.

var interval;

function Play() {
  interval = setInterval(Playimages, 100);
}

You have to assign the interval variable in setInterval

interval = setInterval(Playimages, 100);
clearInterval(interval);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top