Javascript 使用clearTimeout时的作用域问题

Javascript 使用clearTimeout时的作用域问题,javascript,Javascript,我正在使用setTimeout运行幻灯片放映。我想要一个按钮来阻止它。标记为: <h3> Modest Mouse at Fillmore in Miami <h3> <div> <img id="photo" src="http://the305.com/blog/wp-content/uploads/2014/05/modestmouse3.jpg" alt= "Modest Mouse at Fillmore">

我正在使用setTimeout运行幻灯片放映。我想要一个按钮来阻止它。标记为:

<h3> Modest Mouse at Fillmore in Miami <h3>

  <div>
     <img id="photo" src="http://the305.com/blog/wp-content/uploads/2014/05/modestmouse3.jpg" alt= "Modest Mouse at Fillmore">
      <span>&bull;</span>
      <span>&bull;</span>
      <span>&bull;</span>

  </div>

  <button onclick="stopShow();">Stop</button>
迈阿密菲尔莫尔的普通鼠标
&公牛;
&公牛;
&公牛;
停止
JS是:

var aSlideShowPics = ["http://the305.com/blog/wp-content/uploads/2014/05/modestmouse3.jpg",
  "http://the305.com/blog/wp-content/uploads/2014/05/modestmoude7.jpg",
  "http://the305.com/blog/wp-content/uploads/2014/05/modestmouse8.jpg"
];

  // Timer for SlideShow

 var i = 0;
  function changeSlide()  {  
           var stopShowID;
           stopShowID = window.setTimeout(function()  {
              newPic = aSlideShowPics[i];     
              $("#photo").attr("src", newPic);
              i++;
              if (i < aSlideShowPics.length)  {
                 changeSlide();  // recursive call to increment i and change picture in DOM.
              }  else  {
                       i = 0;  // reset loop to keep slideshow going
                       changeSlide(); // Recursive call on loop end
                 }

              function stopShow() {
                window.clearTimeout(stopShowID);
              }
         }, 3000)
  }

 changeSlide();
var aSlideShowPics=[”http://the305.com/blog/wp-content/uploads/2014/05/modestmouse3.jpg",
"http://the305.com/blog/wp-content/uploads/2014/05/modestmoude7.jpg",
"http://the305.com/blog/wp-content/uploads/2014/05/modestmouse8.jpg"
];
//幻灯片放映计时器
var i=0;
函数changeSlide(){
var-stopShowID;
stopShowID=window.setTimeout(函数(){
newPic=aSlideShowPics[i];
$(“照片”).attr(“src”,newPic);
i++;
如果(i

我在点击“无止损”按钮时不断收到一个参考错误。我曾尝试在代码中的几个地方使用clearTimeout函数,但都出现了相同的错误。也许一双新的眼睛可以看到我的错误。这是你的电话号码。感谢您的任何输入。

将停止秀移到超时和更改幻灯片之外

var stopShowID;
function changeSlide()  { 
    stopShowID = window.setTimeout( function(){}, 3000);       
}
function stopShow() { 
    if(stopShowID) {
        window.clearTimeout(stopShowID);
        stopShowID = null;
    }
}

我无法启动jsfidle示例,因此我更新了代码的内容,出现了两个问题:

1-您的
stopShow
未定义,因此我将其附加到窗口范围: window.stopShow=stopShow

2-对于ClearTimeout作用域问题:您的
stopShowID
变量在您的函数中
changeSlide
:您的
stopShow
正在使用本地副本。我基本上把它作为一个全局变量,这样两个函数都可以访问它。

var aSlideShowPics = ["http://the305.com/blog/wp-content/uploads/2014/05/modestmouse3.jpg",
  "http://the305.com/blog/wp-content/uploads/2014/05/modestmoude7.jpg",
  "http://the305.com/blog/wp-content/uploads/2014/05/modestmouse8.jpg"
];

  // Timer for SlideShow

var stopShowID; 
 var i = 0;

function stopShow() {
    window.clearTimeout(stopShowID);
}

window.stopShow = stopShow;

  function changeSlide()  {  

           stopShowID = window.setTimeout(function()  {
              newPic = aSlideShowPics[i];     
              $("#photo").attr("src", newPic);
              i++;
              if (i < aSlideShowPics.length)  {
                 changeSlide();  // recursive call to increment i and change picture in DOM.
              }  else  {
                       i = 0;  // reset loop to keep slideshow going
                       changeSlide(); // Recursive call on loop end
                 }


         }, 3000)
  }


 changeSlide();
var aSlideShowPics=[”http://the305.com/blog/wp-content/uploads/2014/05/modestmouse3.jpg",
"http://the305.com/blog/wp-content/uploads/2014/05/modestmoude7.jpg",
"http://the305.com/blog/wp-content/uploads/2014/05/modestmouse8.jpg"
];
//幻灯片放映计时器
var-stopShowID;
var i=0;
函数stopShow(){
clearTimeout(stopShowID);
}
window.stopShow=stopShow;
函数changeSlide(){
stopShowID=window.setTimeout(函数(){
newPic=aSlideShowPics[i];
$(“照片”).attr(“src”,newPic);
i++;
如果(i
正在工作的JSFIDLE:

stopShow()方法在窗口级别不存在,它只存在于changeSlide()的主体中。直接贴在窗户上

window.stopShow = function() ...
或者把它从封口中拔出来

var i = 0;
var stopShowId;
function stopShow() {
     window.clearTimeout(stopShowID);
}

function changeSlide()  {  
     stopShowID = window.setTimeout(function()  {
         if (i >= aSlidesShowPics.length - 1)
             i = 0;

         var newPic = aSlideShowPics[i++];     
         $("#photo").attr("src", newPic);
         changeSlide();       
      }, 3000);
  }

为什么stopShow在超时时间内?这就是你的问题我认为这是个问题,但当我把它移出changeslide的全局范围和相同的错误…我只是这样做了,但仍然没有定义stopShow。因为你的fiddle设置为运行ondomready,所以函数不在全局范围内。只是要明确一点,我在body中设置了它,但它仍然没有。但是谢谢你的意见。如果有人投票否决了这个问题,我将不胜感激。谢谢。我认为函数不在作用域内,确实从局部作用域移到了全局作用域,但我仍然得到了引用错误。但感谢这里的每一个人,我需要对这件事进行精神检查。我希望投票再次被重新考虑,因为我已经有机器人警告我了。