私はスライダーライブラリを行っています。私はオブジェクト(私はモジュールパターンを使用しています)を1回だけ使用できるという事実については混乱しています。私がお見せしましょう:setIntervalがスクリプトをブロックしていますか?
let PapaSlide = (function(d) {
'use strict';
let _options = {}, _container, _items, _actIndex, _prevIndex;
let _setOptions = function(opt) {
return {
container: opt.container || 'papa-container',
items: opt.items || 'papa-item',
transitionDuration: opt.transitionDuration || '300',
transitionFunction: opt.transitionFunction || 'ease-in',
timeInterval: opt.timeInterval || '3000',
animationType: opt.animationType || 'fade',
type: opt.type || 'auto',
startAt: opt.startAt || 0
}
};
let _setIndexes = function() {
_options.startAt = _options.startAt > _items.length - 1 ? 0 : _options.startAt;
_actIndex = _options.startAt;
_prevIndex = (_actIndex === 0) ? _items.length - 1 : _actIndex - 1;
};
let _addTransitionStyle = function() {
_items.forEach(item => {
item.style.transitionDuration = `${_options.transitionDuration}ms`;
item.style.transitionTimingFunction = _options.transitionFunction;
});
};
let _sliderType = function() {
_setIndexes();
if(_options.animationType === 'fade' && _options.type === 'auto') {
_autoFade();
}
};
let _autoFade = function() {
_items[_actIndex].style.opacity = 1;
setInterval(() => { // is this blocking my other sliders?
_prevIndex = _actIndex;
_actIndex++;
if(_actIndex > _items.length - 1) {
_actIndex = 0;
}
_items[_prevIndex].style.opacity = 0;
_items[_actIndex].style.opacity = 1;
}, parseInt(_options.timeInterval));
};
let setPapaSlider = function(opt) {
_options = _setOptions(opt);
_container = d.getElementsByClassName(_options.container)[0];
if(_container) {
_items = Array.prototype.slice.call(_container.getElementsByClassName(_options.items));
if(_items.length > 0) {
_addTransitionStyle();
_sliderType();
}
else {
console.error('Items have been not found');
}
}
else {
console.error('Container has been not found');
}
};
return {
setPapaSlider: setPapaSlider
}
})(document);
と私のmain.js
(function(PapaSlide) {
"use strict";
PapaSlide.setPapaSlider({container: 'fade-auto', timeInterval: '1200'});
PapaSlide.setPapaSlider({container: 'fade-self', timeInterval: '2000'});
})(PapaSlide || {});
実は、「フェードイン自己」クラスを持つだけで、コンテナが「オートフェード」、スライドさが停止されます。 javascriptにはスレッドが1つあり、setIntervalは別のPapaSlideアクションをブロックしているためですか?私はコンソールのオプションでconsoledしていると私は引数を入力するオプションを持っているので、..?私はどこかでclearIntervalを使うべきですか?しかし、これらのスライダーは不規則なので、私はできないと思う。
編集
OK。私はこのようなことをしました。私はIFFE関数から削除しました。ちょうどタイプされた:
let fade1 = new PapaSlide();
fade1.setPapaSlide({container: 'fade-auto'});
let fade2 = new PapaSlide();
fade2.setPapaSlide({container: 'fade-self'});
これは良い解決策ですか?
私があなたが書き留めた元の機能があなたに失敗した理由を答えに書きました。あなたの編集は間違いなく問題を解決します。つまり、グローバルな 'document'変数で' PapaSlide() 'を呼んでいると仮定しています。また、' new'演算子は必要ありません(PapaSliderは[コンストラクタ](http: /bonsaiden.github.io/JavaScript-Garden/#function.constructors)。 – javinor
これはes6で書かれていますか? – sabithpocker