業務邏輯: 下載txt文件,因數據量較大,等待時間過長,需要做頁面等待提示,下載完成後要關閉,並沒做進度條提示
解決思路:reponse寫一個cookie過來 每次下載完 更新cookie的值 頁面定時檢查cookie值是否更新
代碼
//檢測cookie函數,如果cookie爲空則跳轉到登陸頁
function checkCookie() {
var fileDownloadStatus = getCookie('fileDownloadStatus');
var oldFileStatus = $("#fileDownloadStatus").val();
if((oldFileStatus.length == 0 && fileDownloadStatus.length > 0)
|| (oldFileStatus.length > 0 && fileDownloadStatus.length > 0 && fileDownloadStatus != oldFileStatus)) {
$("#fileDownloadStatus").val(fileDownloadStatus);
closeLoading();
}
}
function getCookie(cookieName) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for(var i = 0; i < arrCookie.length; i++){
var arr = arrCookie[i].split("=");
if(cookieName == arr[0]){
return arr[1];
}
}
return "";
}
<input type="hidden" id="fileDownloadStatus"/>
//每隔1秒,執行cookie檢測函數
setTimeout("checkCookie()",1000);