Javascript $http.get请求中有多个$http.get请求未正常运行
我尝试过不同的方法,但都失败了。问题是我有一个http.get请求,它查询给定一天的数据(全天的总数),返回后,我在.then()中运行多个http.get请求,以将一天拼接到用户提供的班次中。问题是,假设我有七天的时间,我想查询,每天3班。我运行查询,当一天结束时,我将其登录到一个对象中。这三个http请求是同步运行的,应该推到键shifts下嵌套的那一天。接下来会发生的是前几天的工作,但奇怪的是,它会将一个班次推到第二天,使第二天的班次总数为4,而不是3。其他时候,它会在第一天轮班,第二天轮班两次。它永远不会超过总轮班数,在本例中为21,而是在一天中保持平衡。你知道怎么做吗。目前我正在做的事情看起来像这样 当天的主要功能:Javascript $http.get请求中有多个$http.get请求未正常运行,javascript,angularjs,http,http-get,angularjs-http,Javascript,Angularjs,Http,Http Get,Angularjs Http,我尝试过不同的方法,但都失败了。问题是我有一个http.get请求,它查询给定一天的数据(全天的总数),返回后,我在.then()中运行多个http.get请求,以将一天拼接到用户提供的班次中。问题是,假设我有七天的时间,我想查询,每天3班。我运行查询,当一天结束时,我将其登录到一个对象中。这三个http请求是同步运行的,应该推到键shifts下嵌套的那一天。接下来会发生的是前几天的工作,但奇怪的是,它会将一个班次推到第二天,使第二天的班次总数为4,而不是3。其他时候,它会在第一天轮班,第二天轮
function nextDay(startDate, endDate, processObj) {
//console.log('I am in the next()');
d = startDate;
if (d <= endDate) {
//console.log();
da = new Date(d);
da.setDate(da.getDate() + 1);
$http.get(WEB CALL HERE).then(function(response) {
// Store the username, get the profile.
dataAggregator(response.data, d, da);
}).then(function() {
if ($scope.reporting.shiftsEnabled === true) {
var tempPromise = $q.defer();
var tempCntr = 0;
nextShift(tempPromise, tempCntr, startDate, endDate, processObj);
} else {
d.setDate(d.getDate() + 1);
nextDay(d, endDate, processObj);
}
}, function(error) {
console.log('Failure...', error);
});
函数下一天(开始日期、结束日期、处理对象){
//log('我在下一个()中');
d=起始日期;
如果(d我最终解决了自己的问题,重新编写了一个接一个地运行的函数,而不是在彼此内部运行…使用$Q-谷歌搜索它。我不想成为一个傻瓜,我只是不太理解它,无法简洁地解释,但这是为了延迟执行。我真的不明白你为什么要这样做。@VSO已经是了在每一个班次的行中使用$q,其中表示var thePromise=$q.defer();并且我有另一个用于我对日数据进行的主要调用。不确定您是否认为我还需要它?我也愿意接受其他方式,只需要这一点就可以了
function nextShift(shiftPromise, cntr, startDate, endDate, processObj) {
var d = startDate;
if (cntr < $scope.shiftsObj.length) {
var weekday = new Array(7);
weekday[0] = "sunday";
weekday[1] = "monday";
weekday[2] = "tuesday";
weekday[3] = "wednesday";
weekday[4] = "thursday";
weekday[5] = "friday";
weekday[6] = "saturday";
tempStartDate = new Date($scope.shiftsObj[cntr].startTime);
tempStartDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempStartDate.getHours(), tempStartDate.getMinutes(), 0, 0);
tempEndDate = new Date($scope.shiftsObj[cntr].endTime);
if ($scope.shiftsObj[cntr].endTime < $scope.shiftsObj[cntr].startTime) {
tempEndDate = new Date(da.getFullYear(), da.getMonth(), da.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
} else {
tempEndDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
}
var webShiftPromise = $q.defer();
var webShiftCallReturn = webShiftCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
processObj, tempStartDate, tempEndDate,
$scope.reporting.hardware, webShiftPromise, cntr);
webShiftCallReturn.then(function(retData) {
var thePromise = $q.defer();
shiftDataAggregator(retData, tempStartDate, tempEndDate, shiftPromise, cntr);
}, function(error) {
console.log('Failure...', error);
});
cntr++;
nextShift(shiftPromise, cntr, startDate, endDate, processObj);
} else {
d.setDate(d.getDate() + 1);
shiftPromise.resolve(nextDay(d, endDate, processObj));
}
return shiftPromise.promise;
}