AngularJS '$timeouts
까지 기다리는 방법은 무엇입니까?
for (var i = 0; i < 1000; i++) {
$timeout(function() {
//do sth
}, 100);
};
//then do something else is all $timeouts are done
AngularJS '$timeouts
까지 기다리는 방법은 무엇입니까?
for (var i = 0; i < 1000; i++) {
$timeout(function() {
//do sth
}, 100);
};
//then do something else is all $timeouts are done
당신은 시간 제한에 의해 당신이 그것을 달성 할 수 $q.all
의 조합 ($q를 주입) 반환 약속을 사용할 수 있습니다.
예 : -
var promises = [];
for (var i = 0; i < 1000; i++) {
promises.push(performTask(i)); //push promise to the array
}
//If in your real case i is actually an array of something then you can
//just simplify it to $q.all(myArrayOfInputs.map(performTask)).then(...)
$q.all(promises).then(performDoneTask); //use q.all to wait for all promises to be fulfilled.
//Method that does something once all the timeouts are completed
function performDoneTask(){
}
//Method that does something with i and returns a promise from the timeout
function performTask(i){
return $timeout(function() {
//do sth
}, 100);
}
감사합니다. 지금까지는 ($ q를 사용하여) 맞춤 약속을 작성하고 그 안에 모든 다른 사람들을 고지 등으로 둘러 쌉니다. 제 목적 중 일부는 래퍼 프로미즈가 더 좋습니다. 어쨌든 감사합니다. – Seti
@seti $ q를 사용하여 지연된 개체를 만드는 것을 사용자 지정 약속으로하고 있습니까? 시간 초과가 이미 약속을 반환하기 때문에 (실제로 다른 옵션이없는 한) 당신은 할 필요가 없습니다. 이것을 읽으십시오 http://stackoverflow.com/questions/23803743/what-is-the-deferred-antipattern-and-how-do-i-avoid-it – PSL
그렇습니다, $ q로 defered. – Seti
나는이에 대한 기본 솔루션을 생각할 수 없다, 그러나 당신이 설정할 때 시작 방법에 대한 모든 호출이 (라고하는 비동기 매니저의 일종을 구현하는 것은 매우 간단 타이머)는 카운터를 증가시키고 모든 호출은 stop 메소드 (비동기 함수가 완료 될 때 호출 됨)를 감소시킵니다. 카운터가 0으로 돌아 오면 최종 콜백이 트리거 될 수 있습니다. – doldt