내가 실제로 활성 요청이 있고 기다리고 경우보고 공장이나 서비스를 수행하는 것이 좋습니다,이보십시오.
yourApp.factory('apiReqeust', ['$http', function($http) {
// If you want to complete all requests that you sending,
// make this varable an empty array and push every new request inside it.
var waitingRequest = null; // Or quenuedRequests = [];
var isWorking = false;
// In my case I will not make an array, and will have only one request that will wait.
// I mean the last attempted request.
// This is perfect for an Autocomplete API
function request(options, callback) {
if (isWorking) {
// If you are already running request at this time.
// Replace waiting request with this one. (this one is newer request)
waitingRequest = {options: options, callback: callback}
// EDIT! I forgot about the return statement.
// You need to stop, otherwise the request will be executed anyway.
return;
}
isWorking = true;
$http(options).success(function(data) {
// When the request ends, tell that you are not working
// So you can execute next request immediately
isWorking = false;
// execute callback with data parameter.
callback(data);
// If you have waiting request yet. execute it.
if (waitingRequest) {
request(waitingRequest.options, waitingRequest.callback);
waitingRequest = null;
}
}).error(function(err) {
isWorking = false;
callback(null, err);
if (waitingRequest) {
request(waitingRequest.options, waitingRequest.callback);
}
});
}
return request;
});
이것의 사용법은 다음과 같습니다
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
options = {'Some': 'HttpOptions'};
apiRequest(options, function(data, err) {
if (err) throw new Error(err);
// And here you doing what you want with the data.
});
}]);
이 코드를 테스트하고 있지 않다. 작동하는지 모르겠습니다. 그러나 당신이 그 아이디어를 얻길 바랍니다.
? 귀하의 질문은 명확하지 않습니다. – Jan
@Jan 요청을하는 동안 지연을 추가하고 싶습니다.이 링크를 확인하십시오. http://s6.postimg.org/bw2uuera9/svo.png – droidev
실행중인 경우 해당 기능을 비활성화 할 수 있습니다. –