我正在try 同步日历,但接收服务器只想要"一口大小"的数据,所以我决定每月同步每个用户.
因此,我做了一个双循环:
首先,我循环遍历所有用户,如果日期范围跨越多个月,则在该循环中循环遍历月份.
但是,因为同步函数是异步的,所以它同时执行多个资源和月份,因为循环不等待完成.
我知道以前也有人问过类似的问题,但出于某种原因,我就是不能让它起作用.
以下是我的功能:
function loopThroughMonths(resourceIds, startDate, endDate) {
startDateObject = new Date(startDate);
endDateObject = new Date(endDate);
// Check how many months our date range spans:
var dateRangeMonths = monthDiff(startDateObject, endDateObject);
if (dateRangeMonths > 0) {
// Loop through each month
for (let i = 0; i <= dateRangeMonths; i++) {
if (i == 0) {
// For the first month, the starting date is equal to the start of the date range
var loopStartDate = startDate;
var loopEndDate = formatDate(
lastDayOfMonth(
startDateObject.getFullYear(),
startDateObject.getMonth(),
),
);
}
if (i != 0 && i != dateRangeMonths) {
var loopMonth = new Date(
startDateObject.getFullYear(),
startDateObject.getMonth() + i,
1,
);
var loopStartDate = formatDate(
firstDayOfMonth(loopMonth.getFullYear(), loopMonth.getMonth()),
);
var loopEndDate = formatDate(
lastDayOfMonth(loopMonth.getFullYear(), loopMonth.getMonth()),
);
}
if (i == dateRangeMonths) {
// For the last month, the end date is equal to the last date of the date range.
var loopStartDate = formatDate(
firstDayOfMonth(
endDateObject.getFullYear(),
endDateObject.getMonth(),
),
);
var loopEndDate = endDate;
}
loopThroughResources(resourceIds, 0, loopStartDate, loopEndDate);
}
} else {
// Date range falls within 1 month, just proceed to looping through resources
loopThroughResources(resourceIds, 0, startDate, endDate);
}
}
function loopThroughResources(resourceIds, i, loopStartDate, loopEndDate) {
if (i == resourceIds.length) {
$("#start_exchange")
.text("Synchroniseren")
.removeAttr("disabled")
.css("cursor", "pointer");
return;
}
var resourceId = resourceIds[i];
$("#exchange_output").append(
"Start synchroniseren naar Outlook-agenda van " +
resourceNames.get(resourceId) +
" van " +
loopStartDate +
" tot " +
loopEndDate +
"...<br>",
);
$.post(
"sync.php",
{
resourceId: resourceId,
startDate: loopStartDate,
endDate: loopEndDate,
},
function (response) {
$("#exchange_output").append(response);
i = i + 1;
loopThroughResources(resourceIds, i, loopStartDate, loopEndDate);
},
);
}
因此,我来解释一下:
LoopThroughMonths首先判断startDate和endDate是否相差0个月以上. 如果是这样的话,它每个月都会看一遍.如果不是,它会立即执行loopThroughResources.
如果日期RangeMonth跨越多个月,我们使用for循环遍历它们,并 for each 月执行loopThroughResources函数.
因此,如果我们说:
从2023-12-27到2024-02-16同步资源A、B、C
它将做到:
Sync 2023-12-27 till 2023-12-31 (part of december 2023) for resource A
Sync 2023-12-27 till 2023-12-31 (part of december 2023) for resource B
Sync 2023-12-27 till 2023-12-31 (part of december 2023) for resource C
Sync 2024-01-01 till 2024-01-31 (whole january 2023) for resource A
Sync 2024-01-01 till 2024-01-31 (whole january 2023) for resource B
Sync 2024-01-01 till 2024-01-31 (whole january 2023) for resource C
Sync 2024-02-01 till 2024-02-16 (part of february 2023) for resource A
Sync 2024-02-01 till 2024-02-16 (part of february 2023) for resource B
Sync 2024-02-01 till 2024-02-16 (part of february 2023) for resource C
代码可以工作,但它不会等待所有资源完成(即在loopThroughResources函数完成之前),然后才继续下个月.
对于资源,我甚至通过从$.post Complete函数调用函数,使其等待资源A的同步完成,然后才继续到资源B,但基本上我需要另一个等待整个loopThroughResources函数(我猜它需要Promiss.all...)
我知道我必须做一些promise ,但我就是不能让它工作. 任何帮助将不胜感激.