2 回答
TA贡献1793条经验 获得超6个赞
感觉就是个闭包的问题,你试试下面的代码:
function swiperInit(option,time){
console.log(option);
var i = 0
for(var key in option) {
setTimeout((function(key){
return function() {
new Swiper(key, {
direction: 'horizontal',
loop: true,
autoplay: time,
speed: 2000,
effect: option[key],
});
}
})(key),500 * i++);
}
}
swiperInit(option,advertTime);
TA贡献1853条经验 获得超18个赞
闭包问题 在 for里面写一个匿名函数就可以了
for(var currentKey in obj){
(function (key) {
setTimeout(function () {
new Swiper(key, {
direction: 'horizontal',
loop: true,
autoplay: time,
speed: 2000,
effect: option[key],
})
}, 500);
})(currentKey);
}
添加回答
举报