シンプルなJavaScriptタイマー

こんにちは、6か月前にアーカイブで大騒ぎして、小さなテストシステムでカウントダウンタイマーとして機能するはずの興味深いスクリプトを1つ見つけました。批判してください。





 //   4  // 1 -   // 2 - id        // 3 -        // 4 -       //       //    function Timer(seconds, element, callback, stopcallback) { this.total = parseInt(seconds) || 3600; if (typeof callback != 'function') throw new Error('Third argument was expecting function callback'); this.callback = callback; //    element   id  this.element = document.getElementById(element) || null; this.stopcallback = stopcallback; } Timer.prototype = { hours : 0, minutes : 0, seconds : 0, //    //    t : null, init : function () { this.tick(); }, tick : function () { self = this; //   this.t = setTimeout(self.tick(), 1000); //     // this.convert(); this.total -= 1; }, convert : function () { this.hours = parseInt(this.total / 3600); this.minutes = parseInt((this.total % 3600) / 60); this.seconds = parseInt((this.total % 3600) % 60); if (this.hours < 10) this.hours = '0' + this.hours; if (this.minutes < 10) this.minutes = '0' + this.minutes; if (this.seconds < 0) this.seconds = 0; if (this.seconds < 10) this.seconds = '0' + this.seconds; this.stopcallback.apply(this); //    if (this.element) { this.element.innerHTML = ''; this.element.innerHTML = this.hours + ':' + this.minutes + ':' + this.seconds; } //        //        if (this.total <= 0 || this.total < 0) { this.total = 0; this.callback.call(); clearTimeout(this.t); return; } }, stop : function() { clearTimeout(this.t); } } //  var countDown = new Timer(130, 'someElement', doSomeActions, stopTimer); countDown.init(); 
      





// 4 // 1 - // 2 - id // 3 - // 4 - // // function Timer(seconds, element, callback, stopcallback) { this.total = parseInt(seconds) || 3600; if (typeof callback != 'function') throw new Error('Third argument was expecting function callback'); this.callback = callback; // element id this.element = document.getElementById(element) || null; this.stopcallback = stopcallback; } Timer.prototype = { hours : 0, minutes : 0, seconds : 0, // // t : null, init : function () { this.tick(); }, tick : function () { self = this; // this.t = setTimeout(self.tick(), 1000); // // this.convert(); this.total -= 1; }, convert : function () { this.hours = parseInt(this.total / 3600); this.minutes = parseInt((this.total % 3600) / 60); this.seconds = parseInt((this.total % 3600) % 60); if (this.hours < 10) this.hours = '0' + this.hours; if (this.minutes < 10) this.minutes = '0' + this.minutes; if (this.seconds < 0) this.seconds = 0; if (this.seconds < 10) this.seconds = '0' + this.seconds; this.stopcallback.apply(this); // if (this.element) { this.element.innerHTML = ''; this.element.innerHTML = this.hours + ':' + this.minutes + ':' + this.seconds; } // // if (this.total <= 0 || this.total < 0) { this.total = 0; this.callback.call(); clearTimeout(this.t); return; } }, stop : function() { clearTimeout(this.t); } } // var countDown = new Timer(130, 'someElement', doSomeActions, stopTimer); countDown.init();









最高で、

スルタン



All Articles