1

i used this code for pause timer:

when using $timeout in angularjs how to implement pause and resume functionality

$scope.pause = function(){
             window.clearTimeout(t);

        }

timedcounter = function () {
       time = time + 1;
       localStorage.autotimertime = time;
       t = $timeout(timedcounter, 1000);
       display(time);
}

2 Answers 2

1

In this snippet, I am showing the time and a button is used to stop/restart the timeout.

You should add your logic inside the onTimeout function

var app = angular.module('myapp', []);
app.controller('myCtrl', function($scope, $timeout) {
  $scope.time = 0;
  $scope.stopped = false;

  $scope.onTimeout = function() {
    $scope.time = $scope.time + 1;
    //Your logic here
    //localStorage.autotimertime = time;
    //display(time);
    mytimeout = $timeout($scope.onTimeout, 1000);

  }

  var mytimeout = $timeout($scope.onTimeout, 1000);

  $scope.stopTimeout = function() {
    $scope.stopped = true;
    $timeout.cancel(mytimeout);
  }

  $scope.restatTimeout = function() {
    $scope.stopped = false;
    mytimeout = $timeout($scope.onTimeout, 1000);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myCtrl">
  <p ng-bind="time"></p>
  <button ng-click="stopTimeout()" ng-show="!stopped">STOP ME !</button>
  <button ng-click="restatTimeout()" ng-show="stopped">RESUME ME!</button>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

@GauravKumar No problem, don't forget to mark it as the answer
@GauravKumar Dont forget to mark it as the answer for future viewers
0

To cancel a timeout request, call $timeout.cancel(promise).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.