AJAX 进度条实现代码

2016-02-19 11:00 15 1 收藏

下面是个超简单的AJAX 进度条实现代码教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

【 tulaoshi.com - Web开发 】

效果如下:

代码如下:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
titleAjax 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; i10; i++)
{
var elem = document.getElementById("block"+i);
elem.innerHTML = clear;
elem.style.backgroundColor = "white";
}
}
/script
/head
body
h1Ajax Progress Bar Example/h1
Launch long-running process:
input type="button" value="Launch" id="go" onclick="go()"/
p
table style="text-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
trtd style="text-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 = "key1/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);
}
}

来源:http://www.tulaoshi.com/n/20160219/1596322.html

延伸阅读
本文实例绘制了炫酷的下载进度条,分享给大家供大家参考,具体内容如下 一、实现思路 1、要实现绘图,通常需要自定义一个UIView的子类,重写父类的- (void)drawRect:(CGRect)rect方法,在该方法中实现绘图操作 2、若想显示下载进度,只需要实例化自定义子类的对象(若是storyboard中控件,只需修改控件的class属性为自定义子类的类名即可...
标签: flash教程
1。首先在场景上画上一个长条状的进度条 2。选取这个进度条之后按 F8 把它转换成Movieclip影片,记得“注册点”必须在左边 3。转换成影片之后设定实例名 loader ,再来编辑这个影片 4。制作100个帧的的进度,然后增加一个图层并增加一个停止播放的指令 5。回到场景,在第一帧输入以下代码 6。在第二帧开始加入你们的...
代码如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Diagnostics; using System.Threading; namespace WindowsApplication2 { /// summary /// Form1&...
标签: Web开发
与现在那些Ajax框架比较,优劣之处?看完再说吧: 1、Ajax.js 代码如下: /*     AJAX v1.4     HJF 2009-7-5 */ function AjaxDO(){     this.HttpRequest = null;     this.openMethod = null; //HTTP请求的方法,为Get、Post 或者Head  ...
标签: Web开发
兼容: opera 9.6 + chrome 2.0 + FF 3 + IE 6 效果:一边输入一边实现验证   环境:ruby 1.8.6 + rails 2.1.0 + windows 核心代码: html: 浏览器禁用javascript时显示提示信息: 代码如下: noscript div style="color:red"您的浏览器不支持javascript,部分功能无法使用/div /noscript 当浏览器禁用JS后,提交按钮不可用,实现客户...

经验教程

703

收藏

38
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部