There are 2 functions
$scope.dateDifference = function(dateBegin, dateFinish) { var dateDifference = ((Math.floor((dateFinish) / 1000)) - (Math.floor((dateBegin) / 1000))); var seconds = dateDifference % 60; dateDifference -= seconds; dateDifference = Math.floor(dateDifference / 60); var minutes = dateDifference % 60; dateDifference -= minutes; dateDifference = Math.floor(dateDifference / 60); var hours = dateDifference % 60; if (hours < 10) hours = '0' + hours; if (minutes < 10) minutes = '0' + minutes; if (seconds < 10) seconds = '0' + seconds; dateDifference = hours + ":" + minutes + ":" + seconds; return dateDifference; } $scope.startTIME = function() { var thisDate = new Date(); var t = ((Math.floor(thisDate / 1000)) - (Math.floor($scope.currentTask.dateBegin / 1000))); var s = t % 60; t -= s; t = Math.floor(t / 60); var m = t % 60; t -= m; t = Math.floor(t / 60); var h = t % 60; if (h < 10) h = '0' + h; if (m < 10) m = '0' + m; if (s < 10) s = '0' + s; if (isStart == 0) $scope.clock = h + ':' + m + ':' + s; clocktimer = $timeout($scope.startTIME, 1000); }
Performing essentially some tasks, how can you reduce the code?
$scope.quickStart=function(tasks){ if (isStart) { $scope.style = { background: 'red' }; $scope.start(); $scope.startTIME(); $scope.tasks.name; isStart = false; $scope.buttonText = "Stop"; $scope.currentTask.name=tasks.name; $scope.currentTask.selectedProject=tasks.selectedProject; } else { $scope.style = { background: 'red' }; $scope.stop(); var restart = $timeout($scope.quickStart, 100); $scope.currentTask.name=tasks.name; $scope.currentTask.selectedProject=tasks.selectedProject; isStart = true; $scope.buttonText = "Stop"; } }
and
var isStart = true; $scope.buttonText = "Start"; $scope.startOrStop = function() { if (isStart) { $scope.style = { background: 'red' }; $scope.start(); $scope.startTIME(); isStart = false; $scope.buttonText = "Stop"; } else { $scope.style = { background: '#11dc51' }; $scope.stop(); isStart = true; $scope.buttonText = "Start"; } };
$scope.startTIME = function() { if (isStart == 0) $scope.clock = $scope.dateDifference($scope.currentTask.dateBegin, new Date()); clocktimer = $timeout($scope.startTIME, 1000); }
$scope.startTIME = function() { if (isStart == 0) $scope.clock = $scope.dateDifference($scope.currentTask.dateBegin, new Date()); clocktimer = $timeout($scope.startTIME, 1000); }
$scope.startTIME = function() { if (isStart == 0) $scope.clock = $scope.dateDifference($scope.currentTask.dateBegin, new Date()); clocktimer = $timeout($scope.startTIME, 1000); }
In general, something tells me that in JS there should be functions that format the date appropriately in text form. - MikeisStart=!isStart
again outside if. - Mike