angularjsangularjs-timeout

Can we update $timeout in AngularJS?


Following is what I'm trying to do. When the first timeout is forcibly cancelled, I want to initiate the second timeout function, without the lag that already exists, had the timeout not been cancelled.

$scope.data1_timeout = $timeout(function() {
  // some action
}, 2000);

$scope.data2_timeout = $timeout(function() {
  // some action
}, 4000);

$scope.show = function() {
  if (some action) {
    $timeout.cancel($scope.data1_timeout); //works perfectly fine
    //But how do I update data2_timeout so that the action inside it occurs just after data1_timeout is cancelled in this block.
  }
}


Solution

  • Easy doing by cancel both $timeout's and call your function manually. Keep it simple =).

    $scope.data1_timeout = $timeout(function() {
      // some action
    }, 2000);
    
    $scope.data2_timeout = $timeout(function() {
      myAction();
    }, 4000);
    
    $scope.show = function() {
      if (some action) {
        $timeout.cancel($scope.data1_timeout);
        $timeout.cancel($scope.data2_timeout);
        myAction();
      }
    }
    
    function myAction () {
      console.log('done');
    }