gpt4 book ai didi

javascript - 从函数获取本地范围并在单击时调用另一个函数 setTimeout

转载 作者:行者123 更新时间:2023-12-03 03:04:56 25 4
gpt4 key购买 nike

如果使用类“variation_swatch”触发点击,我想销毁函数imageGallery的变量 slider 。

阅读了一些有关本地和全局范围的内容,但似乎我无法完成它,因为我发现 thatslider 未定义。

如何正确设置?

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

最佳答案

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

关于javascript - 从函数获取本地范围并在单击时调用另一个函数 setTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com