91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

AJAX中怎么實現一個進度條效果

發布時間:2021-08-07 17:13:31 來源:億速云 閱讀:134 作者:Leah 欄目:web開發

AJAX中怎么實現一個進度條效果,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

效果如下:
AJAX中怎么實現一個進度條效果

 代碼如下:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ajax Progress Bar</title>
<script type="text/javascript">
var xmlHttp;
var key;
var bar_color = 'gray';//進度條的顏色
var span_id = "block";
var clear = "&nbsp;&nbsp;&nbsp;";
function createXMLHttpRequest()//創建XMLHttpRequest對象
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function go()
{
createXMLHttpRequest();//創建XMLHttpRequest對象
checkDiv();//顯示滾動條
xmlHttp.onreadystatechange = callBack;//設置回調函數
var url = "/AjaxDemo/servlet/ProgressBarServlet?task=create";//請求的地址
var button = document.getElementById("go");
button.disabled = true;//設置按鈕不可用
xmlHttp.open("get",url,true);//打開對服務器的連接
xmlHttp.send();//發送請求
}
function callBack()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
setTimeout("pollServer()",500);//定時調用
}
}
}
function pollServer()
{
createXMLHttpRequest();
var url="/AjaxDemo/servlet/ProgressBarServlet?task=poll&key="+key;
xmlHttp.onreadystatechange = pollCallBack;
xmlHttp.open("GET",url,true);
xmlHttp.send();
}
function pollCallBack()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var percent_complete = xmlHttp.responseXML.getElementsByTagName("percent")[0]
.firstChild.data;//從服務器端獲得響應信息
var index = processResult(percent_complete);
for(var i = 1; i<=index; i++)
{
var elem = document.getElementById("block"+i);
elem.innerHTML = clear;
elem.style.backgroundColor = bar_color;
var next_cell = i+1;
if(next_cell > index && next_cell <= 9)
{
document.getElementById("block"+next_cell).innerHTML = percent_complete + "%";
}
}
if(index <9 )
{
setTimeout("pollServer()",500);
}
else
{
document.getElementById("complete").innerHTML = "Complete!";
document.getElementById("go").disabled = false;
}
}
}
}
function processResult(percent_complete)
{
var ind;
if(percent_complete.length == 1)
{
ind = 1;
}
else if(percent_complete.length == 2)
{
ind = percent_complete.substring(0,1);
}
else
{
ind = 9;
}
return ind;
}
function checkDiv()
{
var progress_bar = document.getElementById("progressBar");
if(progress_bar.style.visibility == "visible")
{
clearBar();
document.getElementById("complete").innerHTML = "";
}
else
{
progress_bar.style.visibility = "visible";
}
}
function clearBar()
{
for(var i =1; i<10; i++)
{
var elem = document.getElementById("block"+i);
elem.innerHTML = clear;
elem.style.backgroundColor = "white";
}
}
</script>
</head>
<body>
<h2>Ajax Progress Bar Example</h2>
Launch long-running process:
<input type="button" value="Launch" id="go" onclick="go()"/>
<p>
<table align="center">
<tbody>
<tr>
<td>
<div id="progressBar" >
<span id="block1">&nbsp;&nbsp;&nbsp;</span>
<span id="block2">&nbsp;&nbsp;&nbsp;</span>
<span id="block3">&nbsp;&nbsp;&nbsp;</span>
<span id="block4">&nbsp;&nbsp;&nbsp;</span>
<span id="block5">&nbsp;&nbsp;&nbsp;</span>
<span id="block6">&nbsp;&nbsp;&nbsp;</span>
<span id="block7">&nbsp;&nbsp;&nbsp;</span>
<span id="block8">&nbsp;&nbsp;&nbsp;</span>
<span id="block9">&nbsp;&nbsp;&nbsp;</span>
</div>
</td>
</tr>
<tr><td align="center" id="complete"></td></tr>
</tbody>
</table>
</body>
</html>

代碼如下:


package cn.Ajax.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class ProgressBarServlet extends HttpServlet {
private int counter = 1;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String task = request.getParameter("task");
String res="";
if(task.equals("create")){
res = "<key>1</key>";
counter = 1;
}
else{
String percent = "";
switch (counter) {
case 1: percent = "10";break;
case 2: percent = "23";break;
case 3: percent = "35";break;
case 4: percent = "51";break;
case 5: percent = "64";break;
case 6: percent = "73";break;
case 7: percent = "89";break;
case 8: percent = "100";break;
}
counter++;
res ="<percent>"+percent+"</percent>";
}
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
out.println("<response>");
out.println(res);
out.println("</response>");
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

關于AJAX中怎么實現一個進度條效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

深州市| 太谷县| 镇远县| 湖南省| 金华市| 兴化市| 通渭县| 雅江县| 登封市| 榆林市| 仁化县| 财经| 安新县| 定边县| 江源县| 象州县| 河南省| 仁寿县| 偃师市| 蕉岭县| 南投市| 静乐县| 阿鲁科尔沁旗| 开江县| 潮安县| 汝城县| 临沧市| 绵阳市| 惠东县| 鹰潭市| 林甸县| 故城县| 珠海市| 福鼎市| 河北省| 华池县| 吉安县| 天水市| 广南县| 绍兴市| 会东县|