简体   繁体   English

从一个函数获取本地作用域,然后在另一个函数上调用setTimeout

[英]Get local scope from a function and call at another functions setTimeout on click

I want to to destroy the variable slider of the function imageGallery if a click is triggered with the class "variation_swatch". 如果要通过类“ variation_swatch”触发点击,我想破坏函数imageGallery的变量滑块。

Read some content about local and global scope but it seems that I can not get it done as I get that and slider are not defined. 阅读关于本地和全球范围内的一些内容,但似乎我不能把它做,因为我得到thatslider没有定义。

How can I set it correctly? 如何正确设置?

function imageGallery() {
    var slider = $('#imageGallery').lightSlider({
        gallery:true,
        item:1,
        loop:false,
    });
    var that = this;
}

$(document).on('click','.variation_swatch',function(){
    setTimeout(function(){
        that.slider.destroy();
        console.log('gallery destroyed');
    }, 500);
    setTimeout(function(){
        if (!slider.lightSlider) {
            slider = $('#imageGallery').lightSlider({
                gallery:true,
                item:1,
                loop:false,
            });  
        };
        console.log('gallery rebuilt');
    }, 500);
})

$(document).ready(function () {
    imageGallery();
});

Add the click handler within imageGallery , where you have access to slider : imageGallery添加点击处理程序,您可以在其中访问slider

function imageGallery() {
  var slider = $('#imageGallery').lightSlider({
        gallery:true,
        item:1,
        loop:false,
      });

  $(document).on('click','.variation_swatch',function(){
      setTimeout(function(){
          slider.destroy();
          console.log('gallery destroyed');
      }, 500);
      setTimeout(function(){
          if (!slider.lightSlider) {
              slider = $('#imageGallery').lightSlider({
                  gallery:true,
                  item:1,
                  loop:false,
              });  
          };
          console.log('gallery rebuilt');
      }, 500);
   });
}

$(document).ready(function () {
    imageGallery();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM