对于那些想知道我最终是如何做到这一点的人,我最终做了几个 AJAX 请求。
我的第一个 AJAX 请求确定了需要发出多少个请求(我有 700,000 条数据库记录,我一次做了 30,000 条)并设置了加载栏。设置加载栏后,我点击了相同的 ASPX 文件“X”次,对于每次调用,我都会更新进度条(需要一些数学知识)。一旦进度条达到 100%,我会执行另一个 AJAX 请求以从我所做的服务器端处理中获取结果。
我调用 compare.aspx 并将其附加到 div 的初始代码。
$.ajax({
url: "comparisons.aspx",
type: "GET",
success: getComparison,
error: showErrors
});
//On Success, show results
function getComparison(result) {
//grabs content from comparisons.aspx to show results
$("#differenceSummary").html(result);
}
当 compares.aspx 首次加载时,它会加载一个空的进度条,并根据我上传的文件生成信息(这是特定于我的应用程序的)。加载此页面时,将 AJAX 请求的数量放入 javascript。
//how many records to check at a time
var numOfRecordsAtATime = 30000;
//number of original/modified records
var origNumOfRecords = parseInt($("#origNumOfRecords").text());
var modNumOfRecords = parseInt($("#modNumOfRecords").text());
var highestNum;
//arithmetic to see how many AJAX calls are necessary
if (origNumOfRecords > modNumOfRecords) {
highestNum = origNumOfRecords;
} else {
highestNum = modNumOfRecords;
}
var numberofCallsToMake = parseInt(Math.floor(highestNum / numOfRecordsAtATime)) + 1;
//How much the progress meter should increase at a time
var increments = (100 / numberofCallsToMake);
//number of records in total we've completed
var numRecordsCompleted = 0;
//number of times we've incremented
var numIncrementsCompleted = 0;
function startAjax() {
$("#progressbar").progressbar({
value: 1
});
if (!halt) {
while (true) {
if (numRecordsCompleted < origNumOfRecords) {
$.ajax({
url: "ajaxCall.aspx?randNo=" + Math.random(),
type: "GET",
success: doAjaxCall,
error: showTheError
});
numRecordsCompleted = numRecordsCompleted + numOfRecordsAtATime;
} else {
break;
}
}
}
}
function doAjaxCall(result) {
numIncrementsCompleted++;
console.log(result);
var progress = (parseInt(numIncrementsCompleted * increments));
$("#progressbar").progressbar({
value: progress
});
console.log(progress);
if (progress == 100) {
getResults();
}
}