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"; } }; 
  • At least $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. - Mike
  • @Mike, can you help with two more features? Added to the description - Dmitry
  • In the first function, make the same actions before if, there are many of them. and you can write isStart=!isStart again outside if. - Mike
  • @Mike and how will it look? - Dmitry

2 answers 2

Here is an example of how to do it.

Also for work with dates there is a quite good moment library. Allows you to conveniently format, change and compare dates. Actually, it has the ability to format the date with templates, which is not native in JavaScript.

 var diffTime = function( otherTime ) { var date = new Date( Date.now() - otherTime ) var time = [ date.getHours(), date.getMinutes(), date.getSeconds() ] .map( function( n ) { // Редактирует число в двухзнаковое return ( '0' + n ).substr( -2 ) } ) .join( ':' ) return time } var otherTime = Date.now() - 30000; alert( diffTime( otherTime ) ) 

    Something like this

      $scope.startOrStop = startOrStop; $scope.quickStart = quickStart; $scope.style = {}; function startOrStop() { if (!isStart) { $scope.style.background = '#11dc51'; return stop("Start"); } start(); }; function quickStart(tasks){ $scope.currentTask.name=tasks.name; $scope.currentTask.selectedProject=tasks.selectedProject; if (!isStart) { $scope.style.background = 'red'; var restart = $timeout($scope.quickStart, 100); return stop("Stop"); } start(); }; function start(){ $scope.style.background = 'red'; $scope.start(); $scope.startTIME(); isStart = false; $scope.buttonText = "Stop"; }; function stop(buttonText){ $scope.stop(); isStart = true; $scope.buttonText = buttonText; };