piszę plugin do jquery. do tej pory wygląda to tak:
$.fn.jCounter = function(jsSettings) { var mergedSettings = $.extend(jCGlobalSettings, jsSettings); return this.each(function() { this.element = $(this); this.settings = mergedSettings; //pobiera dane z data-atrybutów elementu for (var key in this.settings) { if (this.element.data( key.toLowerCase() ) != undefined) { this.settings[key] = this.element.data( key.toLowerCase() ); } } console.log(this.settings); //nieistotne w tym kontekście if (typeof(this.settings.animation) !== "function") { this.settings.animation = jCAnimations[this.settings.animation]; if (this.settings.animation !== "function") { this.settings.animation = jCAnimations['basic']; } } if (this.settings.digitsOnRight < 0) { this.settings.digitsOnRight = 0; } this.steps = Math.ceil(this.settings.updatesPerSec * this.settings.duration / 1000); this.stepCounter = 0; //wygenerowanie kolejnej wartości licznika na podstawie danych z this jCUpdateCounter(this); if (typeof(this.settings.onStart) === "function") { this.settings.onStart(this.element); } this.intervalId = setInterval(function(counter) { jCUpdateCounter(counter); if (counter.stepCounter > counter.steps) { clearInterval(counter.intervalId); if (typeof(counter.settings.onEnd) === "function") { counter.settings.onEnd(counter.element); } } }, this.settings.duration / this.steps, this); //przekazanie this do setInterval jako counter }); };
problemem jest to że przy drugiej iteracji pętli $.each nadpisywane jest this.settings pierwszego elementu. dlatego jeżeli plugin odpalanay jest na jednym elemencie jquery wszystko wygląda ok. w innych wypadakach dane się dziwnie mieszają. czy to problem z $.each? może przekazywanie this do setInterval powoduje takie błędy?