我有一个数组。当我循环遍历数组时,我编写请求并进行http调用。但我希望每个http PUT请求都能在16秒内发出。我尝试了n种方法。我尝试用$timeout包装http.then,$interval循环1次,在http配置中添加timeout:16000。它们都没有延迟http put调用。只有循环中的第一个调用被延迟。如何将每个实际的http调用延迟16秒?这是我的密码。我在http配置中添加了timeout以及$timeout。我一次试一个,两个都试。无工作
angular.forEach($scope.provisionDataArray, function(provReq, index) {
var userProvisionSCIMUrl = someurl;
scimProvReq = prepareProvisionRequestJSON(provReq, $scope.refData, $scope.App);
var scimReq = {
method: 'PUT',
url: someurl,
headers: {
'Content-Type': 'application/json'
},
timeout: 16000,
data: scimProvReq
}
$timeout(function() {
$http(scimReq).then(function successCallback(response) {
var provStatus = {};
provStatus.reqNum = index;
provStatus.nbid = response.data.id;
provStatus.id = response.data.request.id;
provStatus.status = response.data.request.status;
provStatus.statusMessage = response.data.request.statusMessage;
$scope.provisionStatus.push(provStatus);
},
function errorCallback(response) {
$scope.errors.push({
error: "Error processing,
line: index
});
});
},16000,$scope.provisionDataArray.length)
}
});
您可以尝试类似的方法。
var scimReq = {
method: 'PUT',
url: someurl,
headers: {
'Content-Type' : 'application/json'
},
timeout: 16000,
data: scimProvReq,
index: 0
}
$scope.doIt = function() {
if($scope.provisionDataArray.length > 0){
$timeout(function(){
$http(scimReq).then(function(){
if(scimReq.index !== $scope.provisionDataArray.length){
scimReq.index = scimReq.index + 1;
$scope.doIt();
}
});
}, scimReq.timeout);
}
}
$scope.doIt();
$interval每隔X毫秒执行其中的内容。代码的问题是,区间内唯一的部分是$http调用,因此它将遍历并立即为每个项调用$interval($http)。
使用递归策略,在上一个请求完成后调用每个请求。
var index = 0
function recursionRequests() {
if (typeof $scope.provisionDataArray[index] == 'undefined') return; // breaking condition when reached our last request
var provReq = $scope.provisionDataArray[index];
var userProvisionSCIMUrl = someurl;
scimProvReq = prepareProvisionRequestJSON(provReq, $scope.refData, $scope.App);
var scimReq = {
method: 'PUT',
url: someurl,
headers: {
'Content-Type': 'application/json'
},
timeout: 16000,
data: scimProvReq
}
$http(scimReq).then(function successCallback(response) {
var provStatus = {};
provStatus.reqNum = index;
provStatus.nbid = response.data.id;
provStatus.id = response.data.request.id;
provStatus.status = response.data.request.status;
provStatus.statusMessage = response.data.request.statusMessage;
$scope.provisionStatus.push(provStatus);
},
function errorCallback(response) {
$scope.errors.push({
error: "Error processing",
line: index
});
});
$timeout(recursionRequests, 16000);
index++;
}
更新:几乎忘记了超时XD
我用下面的代码解决了这个问题。如果我打破循环进行http调用,我想我可以实现延迟。所以我把我的数组切成了一个1大小的块。然后添加了超时。如果我将chunk大小设置为2,则在2次调用后会添加延迟。
var provisionChunks = sliceProvisionArray($scope.provisionDataArray, chunkSize);
angular.forEach(provisionChunks, function(provReqs, index) {
console.log("provReqs ,index ",provReqs + " " + index);
angular.forEach(provReqs, function(provReq, index1) {
setTimeout(function(x) {
return function() {
provisionUsers(provReq, index1, provRequestCount);
};
}(index), 12000*index);
});
});
function sliceProvisionArray(arr, chunkSize) {
var slicedProvArray = [], i;
for (i = 0; i < arr.length; i += chunkSize) {
slicedProvArray.push(arr.slice(i, i + chunkSize));
}
return slicedProvArray;
}
function provisionUsers(provReq, index, provReqCount) {
var userProvisionSCIMUrl = "https://link"
var scimProvReq = prepareProvisionRequestJSON(provReq, $scope.referenceData, $scope.virtualApp);
var scimReq = makePUTSCIMReq(scimProvReq,userProvisionSCIMUrl);
$http(scimReq).then(function successCallback(response) {
}
},
function errorCallback(response) {
}