2 回答

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
感覺(jué)就是個(gè)閉包的問(wèn)題,你試試下面的代碼:
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貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
閉包問(wèn)題 在 for里面寫一個(gè)匿名函數(shù)就可以了
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);
}
添加回答
舉報(bào)