最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - clearIntervalsetInterval - Stack Overflow

programmeradmin0浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论