39 lines
813 B
JavaScript
39 lines
813 B
JavaScript
|
'use strict';
|
||
|
|
||
|
angular.module('view-form').service('TimeCounter', [
|
||
|
function(){
|
||
|
var _startTime, _endTime = null, that=this;
|
||
|
|
||
|
this.timeSpent = 0;
|
||
|
|
||
|
this.restartClock = function(){
|
||
|
_startTime = Date.now();
|
||
|
_endTime = null;
|
||
|
// console.log('Clock Started');
|
||
|
};
|
||
|
|
||
|
this.getTimeElapsed = function(){
|
||
|
if(_startTime) {
|
||
|
return Math.abs(Date.now().valueOf() - _startTime.valueOf()) / 1000;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
this.stopClock = function(){
|
||
|
if(_startTime && _endTime === null){
|
||
|
_endTime = Date.now();
|
||
|
this.timeSpent = Math.abs(_endTime.valueOf() - _startTime.valueOf())/1000;
|
||
|
this._startTime = this._endTime = null;
|
||
|
|
||
|
return this.timeSpent;
|
||
|
}else{
|
||
|
return new Error('Clock has not been started');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
this.clockStarted = function(){
|
||
|
return !!this._startTime;
|
||
|
};
|
||
|
|
||
|
}
|
||
|
]);
|