Angularjs 在另一个函数完成后调用函数

Angularjs 在另一个函数完成后调用函数,angularjs,Angularjs,我需要知道,function1中的$timeout何时完成。如何知道超时何时完成工作 $scope.function1=函数(数组、文件\u id){ 对于(var i=0;i

我需要知道,function1中的$timeout何时完成。如何知道超时何时完成工作

$scope.function1=函数(数组、文件\u id){
对于(var i=0;i});
您只需添加一个标志来检查
$timeout
中的函数是否运行:

var timerRun = false;
$scope.function1 = function (array, file_id) {
   timerRun = false;
   for (var i = 0; i < array.length; i++){
     if (array[i].file_id == file_id){     

       $timeout(function () {
         array.splice(i, 1); 
         // when finish?
         timerRun = true; 
       }, 2000);
       break;
     }
   }
   return array;
}


$scope.function1($rootScope.parcelQeue, response[0]);
getOutboxParcelFactory.getParcel(response).then(function(infoObj){
  // do something if the timeout in function1 is finished
  if (timerRun) {
    doSomething();
  } else {
    console.log("I am not doing anything, and will not do in the future because the timeout in function1 is not finished.");
  }
});
var timerRun=false;
$scope.function1=函数(数组、文件\u id){
timerRun=false;
对于(var i=0;i
这不是一个优雅的解决方案,但我认为它解决了你的问题


但是,如果您不想只检查,还要等待
$timeout
完成,那么您需要一种不同的方法。

您只需添加一个标志来检查
$timeout
中的函数是否运行:

var timerRun = false;
$scope.function1 = function (array, file_id) {
   timerRun = false;
   for (var i = 0; i < array.length; i++){
     if (array[i].file_id == file_id){     

       $timeout(function () {
         array.splice(i, 1); 
         // when finish?
         timerRun = true; 
       }, 2000);
       break;
     }
   }
   return array;
}


$scope.function1($rootScope.parcelQeue, response[0]);
getOutboxParcelFactory.getParcel(response).then(function(infoObj){
  // do something if the timeout in function1 is finished
  if (timerRun) {
    doSomething();
  } else {
    console.log("I am not doing anything, and will not do in the future because the timeout in function1 is not finished.");
  }
});
var timerRun=false;
$scope.function1=函数(数组、文件\u id){
timerRun=false;
对于(var i=0;i
这不是一个优雅的解决方案,但我认为它解决了你的问题


但是,如果您不想只检查而且等待
$timeout
完成,那么您需要一种不同的方法。

您应该使用承诺。将
getpacket()
服务封装在函数中,并从
$timeout()中调用该函数
-因此,您在超时完成其他工作后执行服务。您应该使用承诺。将
getParcel()
服务封装在函数中,并从
$timeout()
中调用该函数-因此,您在超时完成其他工作后执行服务