使用Ajax實(shí)現(xiàn)簡(jiǎn)單的帶百分比進(jìn)度條實(shí)例
需求:當(dāng)進(jìn)行文件長(zhǎng)傳保存等操作時(shí),能在頁(yè)面顯示一個(gè)帶百分比的進(jìn)度條,給用戶(hù)一個(gè)好的交互體驗(yàn)
實(shí)現(xiàn)步驟
JSP頁(yè)面
1.添加table標(biāo)簽
<table id="load" width="700" border="0" align="center" bgcolor="#FAFAFA" cellpadding="0" cellspacing="0" bordercolor="#000000" style="border-collapse:collapse;display:none ">
<tr>
<td><br><br>
<table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#287BCE" style="border-collapse:collapse ">
<tr bgcolor="#F7F7F6">
<td width="20%" height="100" valign="middle">
<table align='center' width='500'>
<tr>
<td colspan='2' align='center' id="progressPersent"><font size="2">
正在進(jìn)行保存,用時(shí)較長(zhǎng),請(qǐng)稍后...
</font>
</td>
</tr>
<tr>
<td id='tdOne' height='25' width=1 bgcolor="blue"> </td>
<td id='tdTwo' height='25' width=500 bgColor='#999999'> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
這個(gè)table標(biāo)簽要隱藏,進(jìn)度條執(zhí)行的時(shí)候再顯示。id為tdOne和tdTwo分別為進(jìn)度條的藍(lán)色和灰色區(qū)域。
2.添加js代碼
/**添加帶百分比的進(jìn)度條*/
var xmlHttp;
//創(chuàng)建ajax引擎
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
}
}
}
}
function loading() {
createXMLHttpRequest();
clearLoad();
var url = "elecCommonMsgAction_progressBar.do";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = createCallback;
xmlHttp.send(null);
}
function createCallback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
//每隔1秒鐘執(zhí)行一次percentServer()方法,直到當(dāng)前訪(fǎng)問(wèn)結(jié)束
setTimeout("percentServer()", 1000);
}
}
}
function percentServer() {
createXMLHttpRequest();
var url = "elecCommonMsgAction_progressBar.do";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updateCallback;
xmlHttp.send(null);
}
function updateCallback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
//獲取XML數(shù)據(jù)中的percent存放的百分比的值
var percent_complete = xmlHttp.responseXML.getElementsByTagName("percent")[0].firstChild.data;
var tdOne = document.getElementById("tdOne");
var progressPersent = document.getElementById("progressPersent");
//改變藍(lán)色區(qū)域的寬度
tdOne.width = percent_complete + "%";
//將百分比的數(shù)值顯示到頁(yè)面上
progressPersent.innerHTML = percent_complete + "%";
//如果計(jì)算獲取的百分比的數(shù)值沒(méi)有達(dá)到100,則繼續(xù)調(diào)用方法,直到操作結(jié)束為止
if (percent_complete < 100) {
setTimeout("percentServer()", 1000);
}
}
}
}
function clearLoad() {
document.getElementById("load").style.display="";
document.getElementById("opperate1").style.display="none";
document.getElementById("opperate2").style.display="none";
}
當(dāng)點(diǎn)擊保存時(shí),執(zhí)行l(wèi)oading()方法。
Action類(lèi)
progressBar()方法
/**
* @throws Exception
* @Name: progressBar
* @Description: 在頁(yè)面執(zhí)行保存后,使用ajax,計(jì)算執(zhí)行的百分比情況,將結(jié)果顯示到頁(yè)面上
* @Parameters: 無(wú)
* @Return: ajax如果不需要跳轉(zhuǎn)頁(yè)面,返回null或者NONE
*/
public String progressBar() throws Exception{
//從session中獲取操作方法中計(jì)算的百分比
Double percent = (Double) request.getSession().getAttribute("percent");
String res = "";
//此時(shí)說(shuō)明操作的業(yè)務(wù)方法仍然繼續(xù)在執(zhí)行
if(percent!=null){
//計(jì)算的小數(shù),四舍五入取整
int percentInt = (int) Math.rint(percent);
res = "<percent>" + percentInt + "</percent>";
}
//此時(shí)說(shuō)明操作的業(yè)務(wù)方法已經(jīng)執(zhí)行完畢,session中的值已經(jīng)被清空
else{
//存放百分比
res = "<percent>" + 100 + "</percent>";
}
//定義ajax的返回結(jié)果是XML的形式
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
//存放結(jié)果數(shù)據(jù),例如:<response><percent>88</percent></response>
out.println("<response>");
out.println(res);
out.println("</response>");
out.close();
return null;
}
save()方法
/**
* @Name: save
* @Description: 保存表單數(shù)據(jù)到數(shù)據(jù)庫(kù)
* @Parameters: 無(wú)
* @Return: String:重定向到system/actingIndex.jsp再查詢(xún)
*/
public String save(){
//模擬:循環(huán)保存150次,方便觀察百分比變化
for(int i=1;i<=150;i++){
elecCommonMsgService.saveCommonMsg(elecCommonMsg);//保存數(shù)據(jù)
request.getSession().setAttribute("percent", (double)i/150*100);
}
//線(xiàn)程結(jié)束,清空session
request.getSession().removeAttribute("percent");
return "save";
}
注意:可以在復(fù)雜的業(yè)務(wù)中將代碼段分成點(diǎn),一個(gè)點(diǎn)的進(jìn)度是占百分之多少,然后存放到Session中,然后通過(guò)ajax調(diào)用服務(wù)從Session中獲取值,返回進(jìn)度并顯示即可。
效果

輸入數(shù)據(jù),點(diǎn)擊【保存】時(shí):

總結(jié)
帶百分比的進(jìn)度條,實(shí)際上是用ajax在保存中開(kāi)啟多個(gè)線(xiàn)程實(shí)現(xiàn)的:
一個(gè)線(xiàn)程,執(zhí)行保存的操作:
1.將百分比計(jì)算出來(lái),放到session中;
2.在線(xiàn)程結(jié)束的時(shí)候,將session清空。
另一個(gè)線(xiàn)程,從session中獲取百分比的內(nèi)容:
1.使用ajax將結(jié)果返回并顯示在頁(yè)面上
以上所述是小編給大家介紹的使用Ajax實(shí)現(xiàn)簡(jiǎn)單的帶百分比進(jìn)度條實(shí)例,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Ajax實(shí)現(xiàn)靜態(tài)刷新頁(yè)面過(guò)程帶加載旋轉(zhuǎn)圖片
當(dāng)然你也可以使用jquery或者ajax寫(xiě)動(dòng)態(tài)加載table的方式來(lái)實(shí)現(xiàn),方式多種多樣,感興趣的朋友可以了解下,希望本文提供的靜態(tài)刷新頁(yè)面對(duì)你學(xué)習(xí)ajax有所幫助2013-02-02
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)...2007-09-09
基于ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)的方法
這篇文章主要介紹了基于ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)的方法,實(shí)例分析了Ajax實(shí)現(xiàn)分頁(yè)的技巧,需要的朋友可以參考下2015-02-02
ajax內(nèi)部值外部調(diào)用不了的原因及解決方法
下面小編就為大家?guī)?lái)一篇ajax內(nèi)部值外部調(diào)用不了的原因及解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
利用ajax傳遞數(shù)組及后臺(tái)接收的方法詳解
這篇文章主要給大家介紹了關(guān)于利用ajax傳遞數(shù)組及后臺(tái)接收的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06

