javascript - clearIntervalsetInterval - Stack Overflow

I am using the following script to display jpg images as an animation:var interval;function Play() {set

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

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

Share Improve this question edited Aug 30, 2012 at 22:50 Gert Grenander 17.1k6 gold badges41 silver badges43 bronze badges asked Aug 30, 2012 at 22:34 hnclhncl 2,3057 gold badges63 silver badges132 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 6

You never assign the interval id to interval. Should be

interval = setInterval(Playimages, 100);

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745193186a4615965.html

相关推荐

  • javascript - clearIntervalsetInterval - Stack Overflow

    I am using the following script to display jpg images as an animation:var interval;function Play() {set

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信