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 = "   ";

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>

<h1>Ajax Progress Bar Example</h1>

Launch long-running process:

<input type="button" value="Launch" id="go" onclick="go()"/>

<p>

<table align="center">

<tbody>

<tr>

<td>

<div id="progressBar" style="padding:2px;border:solid black 2px;visibility:hidden">

<span id="block1">   </span>

<span id="block2">   </span>

<span id="block3">   </span>

<span id="block4">   </span>

<span id="block5">   </span>

<span id="block6">   </span>

<span id="block7">   </span>

<span id="block8">   </span>

<span id="block9">   </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;

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

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();

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

时间: 2025-01-14 11:07:36

AJAX 进度条实现代码的相关文章

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 = '

asp.net 文件上传进度条实现代码

asp教程.net 文件上传进度条实现代码 index.htm就是文件上传页面,提交form给uploadhandler目录下的default.aspx,以实现文件上传. progresshandler目录下三个文件为abort.ashx.genericguid.ashx,handler.ashx功能分别为:根据guid取消正在上传的文件,生成guid,根据guid获取上传信息. 第一步:建立index.htm页面,这个上传页面,需要注意的就是需要一个隐藏的iframe,并且名字为form提交的

jQuery实现进度条效果代码_jquery

在某些特定的场景下,应用进度条效果,可以提高网站的人性化程度,能够让用户能够掌握进度,增加对进度的耐心,否则可能直接导致关闭页面,从来导致网站流失用户,下面就是用过jQuery实现的进度效果代码. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta nam

按钮-求多文件上传,带进度条的代码,?

问题描述 求多文件上传,带进度条的代码,? 用java 在mybaitys +struts2+spring 实现多文件上传 点上传按钮显进度条,上传到百分在再显示出图片.最好兼容多个浏览器. 解决方案 建议用FileUpload组件来实现 JAVA学习提高之---- FileUpload组件实现多文件上传 http://blog.csdn.net/luweifeng1983/article/details/3613544 解决方案二: 文件上传是前端的事,不干mybatis.spring,最多和

Android自定义View实现带数字的进度条实例代码_Android

第一步.效果展示 图1.蓝色的进度条 图2.红色的进度条 图3.多条颜色不同的进度条 图4.多条颜色不同的进度条 第二步.自定义ProgressBar实现带数字的进度条 0.项目结构 如上图所示:library项目为自定义的带数字的进度条NumberProgressBar的具体实现,demo项目为示例项目以工程依赖的方式引用library项目,然后使用自定义的带数字的进度条NumberProgressBar来做展示   如上图所示:自定义的带数字的进度条的library项目的结构图   如上图所

jquery实现模拟百分比进度条渐变效果代码_jquery

本文实例讲述了jquery实现模拟百分比进度条渐变效果代码.分享给大家供大家参考,具体如下: 这里为了便于看到加载百分比,对代码进行了处理,实际使用时并不需要这样. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-mn-bfb-scroll-cha-style-demo/ 具体代码如下: <html> <head> <title>jquery模拟百分比进度条</title> <script

jQuery多文件异步上传带进度条实例代码_jquery

先给大家展示下效果图: ///作者:柯锦 ///完成时间:2016.08.16 ///多文件异步上传带进度条 (function ($) { function bytesToSize(bytes) { if (bytes === 0) return '0 B'; var k = 1024, // or 1000 sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], i = Math.floor(Math.log(bytes)

Android自定义View实现带数字的进度条实例代码

第一步.效果展示 图1.蓝色的进度条 图2.红色的进度条 图3.多条颜色不同的进度条 图4.多条颜色不同的进度条 第二步.自定义ProgressBar实现带数字的进度条 0.项目结构 如上图所示:library项目为自定义的带数字的进度条NumberProgressBar的具体实现,demo项目为示例项目以工程依赖的方式引用library项目,然后使用自定义的带数字的进度条NumberProgressBar来做展示 如上图所示:自定义的带数字的进度条的library项目的结构图 如上图所示:de

基于Blod的ajax进度条下载实现示例代码

普通的浏览器下载 在web开发中,如果要实现下载功能,往往都是使用新开web页面或者是使用iframe的形式.实现起来其实很简单: <a target="_blank" href="download.zip" rel="external nofollow" >点击下载</a> //或者 <iframe style="display:none" src="download.zip"