如何停止图像的幻灯片放映?

时间:2016-01-28 03:55:51

标签: javascript jquery html

function slideshow(){
    var timer = 0;
    if(++index > 6) index = 1;
    $(".banner").attr("src", images[index-1]);
    $(".textfield").text(captions[index-1]);
    $(".num").text(index);
    timer = setTimeout(slideshow,3000);
}   

function stopShow(){
    clearTimeout(timer);
}

$("#show").click(function(){
    slideshow();
});

$("#stopshow").click(function(){
    stopShow();
});

我有两个按钮分别启动和停止幻灯片放映。在上面的代码部分中,当我单击停止幻灯片按钮时,它会对stopShow()函数进行分类,但是setTimeout循环不会在后台停止

2 个答案:

答案 0 :(得分:5)

我认为这与var timer的范围有关。尝试在slideshow()函数之外移动声明。即:

var timer;

function slideshow(){
    timer = 0;
    ....
}

function stopShow(){
    clearTimeout(timer);
}
....  

答案 1 :(得分:0)

也许这会有所帮助?

var doc, IE, E, C, LameSlideshow; // outside onload scope for use on other pages, onload of course
var pre = onload; // if using this way change var pre for other onloads or too much recursion occurs
onload = function(){
if(pre)pre();

doc = document; IE = parseFloat(navigator.appVersion.split('MSIE')[1]);
E = function(id){
  return doc.getElementById(id);
}
C = function(e){
  return doc.createElement(e);
}
LameSlideshow = function(div, slidesArray, duration, fadeTime){
  this.div = div; this.slidesArray = slidesArray; this.frame = 0; this.running = this.fade = false;
  this.duration = duration || 1000;
  this.fadeTime = fadeTime || 1000;
  this.div.style.backgroundImage =  "url('"+slidesArray[1]+"')"; this.div.style.backgroundRepeat = 'no-repeat'; this.imgElement = C('img');
  this.imgElement.alt = this.imgElement.src = slidesArray[0]; this.div.appendChild(this.imgElement);
  this.start = function(){
    var x = this;
    function loop(){
      var t, l = x.slidesArray.length, o = 1, b;
      if(++x.frame === l)x.frame = 0;
      b = x.frame+1;
      if(b === l){
        b = 0;
      }
      x.div.style.backgroundImage = "url('"+x.slidesArray[b]+"')";
      ;
      if(IE && IE < 9){
        x.imgElement.style.filter = 'alpha(opacity=1)';
      }
      else{
        x.imgElement.style.opacity = 1;
      }
      x.imgElement.alt = x.imgElement.src = x.slidesArray[x.frame];
      if(x.fadeTime !== false){
        clearInterval(x.running);
        x.fade = setInterval(function(){
          o = +((o-0.01).toFixed(2));
          if(o >= 0){
            if(IE && IE < 9){
              x.imgElement.style.filter = 'alpha(opacity='+(o*100)+')';
            }
            else{
              x.imgElement.style.opacity = o;
            }
          }
          else{
            clearInterval(x.fade); x.fadeIn = x.duration; x.running = setInterval(loop, x.duration);
          }
        }, (x.fadeTime/100));
      }
    }
    this.running = setInterval(loop, this.duration);
  }
  this.stop = function(){
    clearInterval(this.running); clearInterval(this.fade);
    if(this.fadeTime !== false){
      if(IE && IE < 9){
        this.imgElement.style.filter = 'alpha(opacity=1)';
      }
      else{
        this.imgElement.style.opacity = 1;
      }
    }
  }
}
var slideshow = new LameSlideshow(E('outputDiv'), ['one.png', 'two.jpg', 'three.gif'], 2000, 1500);
var run = 0;
E('startStopToggleId').onclick = function(){
  if(run === 0){
    slideshow.start(); run = 1;
  }
  else{
   slideshow.stop(); run = 0;
  }
}

}