我有一个秒表,我作为一个ember.js控制器运行,它运行起来有点慢,所以随着时间的推移它落后了。试图确定如何最准确地加快速度。不确定这是由浏览器延迟引起的,还是系统不满意以此速度运行此过程。
//timer.js
import Ember from 'ember';
export default Ember.Controller.extend({
state: 'reset',
tick: 0,
seconds: 0,
minutes: 0,
lapTime: 0,
isResetState: Ember.computed.equal('state', 'reset'),
isRunState: Ember.computed.equal('state', 'run'),
isPauseState: Ember.computed.equal('state', 'pause'),
isLapState: Ember.computed.equal('state', 'lap'),
incrementTick(startDate) {
let currentDate = new Date();
if (this.get('isRunState') || this.get('isLapState')) {
this.incrementProperty('tick', currentDate.valueOf() - startDate.valueOf());
Ember.run.next(this, 'incrementTick', new Date());
}
if (this.tick > 999) {
this.set('tick', 0);
this.incrementProperty('seconds', 1);
}
if (this.seconds > 59) {
this.set('seconds', 0);
this.incrementProperty('minutes', 1);
}
},
actions: {
start() {
this.set('state', 'run');
this.incrementTick(new Date());
},
stop() {
this.set('state', 'pause');
},
reset() {
this.set('state', 'reset');
this.set('tick', 0);
this.set('seconds', 0);
this.set('minutes', 0);
},
hold() {
this.set('lapTime', this.get('tick'));
this.set('state', 'lap');
},
continue() {
this.set('state', 'run');
},
},
});