另一个函数完成后,Angular JS Call函数

时间:2015-08-03 10:08:07

标签: angularjs

我需要知道,当function1中的$ timeout完成时。怎么知道,当超时完成工作?



$scope.function1 = function (array, file_id) {
                                  for (var i = 0; i < array.length; i++){
                                        if (array[i].file_id == file_id){     
  
                                         $timeout(function () {
                                                array.splice(i, 1); 
                                           // when finish?
                                          }, 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
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以简单地添加一个标志来检查$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.");
  }
});

这不是一个优雅的解决方案,但我认为它解决了你的问题。

但是,如果您不想仅检查等待完成$timeout,那么您需要采用不同的方法。

相关问题