'use strict'; angular.module('view-form').service('TimeCounter', [ function(){ var _startTime, _endTime = null; 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; } return new Error('Clock has not been started'); }; this.clockStarted = function(){ return !!this._startTime; }; } ]);