나는 최근에 내 자신의 시세 클래스는, 사용자의 요구에 연장 자유롭게 쓴 :
// An advanced timeout class which also calculates the current progress
var Ticker = function(start, duration, callback) {
var _this = this;
this.start = start;
this.duration = duration;
// TODO easing options
this.reverse = false;
this.auto_reset = false;
this.auto_reverse = false;
this.callback = callback ? callback : null;
this.timeout = callback ? setTimeout(function() {
_this.callback();
if (_this.auto_reset || _this.auto_reverse) { _this.reset(); }
}, this.duration) : null;
};
Ticker.prototype.status = function() {
var end = this.start + this.duration;
var current = +new Date;
var duration = end - this.start;
var difference = current - this.start;
var progress = this.reverse ? 1 - (difference/duration) : (difference/duration);
if (current >= end) { progress = this.reverse ? 0 : 1; }
return progress;
};
Ticker.prototype.reset = function() {
if (this.auto_reverse) { this.reverse = this.reverse ? false : true; }
var _this = this;
this.timeout = this.callback ? setTimeout(function() {
_this.callback();
if (_this.auto_reset || _this.auto_reverse) { _this.reset(); }
}, this.duration) : null;
this.start = +new Date;
};
사용법 :
// Tickers every 1000 ms
var ticker = new Ticker(+new Date, 1000, function() {
console.log("done");
});
ticker.auto_reset = true;
또한하여 현재 진행 상황을 얻을 수 있습니다 status()
메서드 호출.
0.0
에서 1.0
까지 현재 진행 상황을 나타내는 숫자를 반환합니다.