我不明白爲什麼MC.caro.myTimer()
在頁面加載上運行,它是一個變量。那麼,如果沒有我的稱呼,這是如何運行的。爲什麼這個JS變量在頁面加載時運行
我認爲這可能與我的自動執行功能有關嗎?
var MC = {};
(function($){
"use strict";
MC.init = function(){
//disable to turn off carousel
MC.caro.init();
};
MC.caro = {
init: function(){
//Build carousel for us to use
MC.caro.build();
//Bind click events to buttons
$('.mc-carousel--nav--buttons').on('click','li',function(){
MC.caro.advance($(this));
//stop the current ticking
MC.caro.stopMyTimer();
//Start up movement after 15seconds
setInterval(function(){MC.caro.myTimer()},10000);
console.info('running');
});
},
build: function(){
MC.caro.jsonRun(function(){
console.info('running1');
});
},
myTimer: function(){
console.info('running2');
MC.caro.advance(nextItem,'slow');
},
//Sets up the timer
timer:setInterval(function(){MC.caro.myTimer()},5000),
stopMyTimer: function(){
clearInterval(MC.caro.timer);
}
}
})(jQuery);
MC.init();
還有一個問題:
當我$('.mc-carousel--nav--buttons').on('click','li',function(){
單擊該代碼將等待15秒鐘,然後運行。如預期。但如果我真的很快點擊它5次,它會等待那些15秒,然後在完全相同的時間運行,並點擊我的點擊,這意味着MC.caro.stopMyTimer();
不起作用,這是我的點擊事件內部調用的。此外,這不是完整的代碼,我不能把它放在小提琴上,因爲它在Intranet上有很大的依賴關係。
所以appoligies,你不能看到一個工作的例子。
這就是我的想法。如果你有時間其他多次運行:) –
嗯,爲什麼它立即評估?像'var x = setInterval(function(){foo()),4000);'不會立即運行「foo」,因爲它爲什麼會這樣呢? – WTK
我現在發生衝突! –