const MILLIS_PER_DAY = 1_000 * 60 * 60 * 24;
let pageSize = 100; // items per page
let currentPage = 0;
async function getNextPage() {
let now = Date.now();
let startTime = now - (currentPage + 1) * MILLIS_PER_DAY; // 1 day earlier
let endTime = now - currentPage * MILLIS_PER_DAY;
try {
let results = await fetchHistoryPage(startTime, endTime, pageSize);
if (results.length) {
currentPage++; // Only when there are results
return results;
} else {
console.log('No more history items.');
}
} catch (error) {
console.error('Error fetching history:', error);
}
return null;
}
使用方法如下:
(async () => {
const allResults = [];
let results;
do {
results = await getNextPage();
if (results) {
allResults.push(...results);
}
} while(results);
// Do something with allResults
})();