ajax進度條

來源:互聯網
上載者:User

1.進度條由DIV框和數個有背景顏色的SPAN組成;
2.初始化時DIV不可見,SPAN背景色與網頁背景色一致;
3.根據伺服器返回的進度百分比,計算出應顯示幾個SPAN塊,並為SPAN塊設定背景色;

progressBar.html:

程式碼<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>ProgressBar</title>
    <script type="text/javascript" src="js/progressBar.js"></script>
</head>
<body>
    <h1>Ajax Progress Bar</h1>
    <input id="btn" type="button" value="Start" onClick="doStart();" />
    <br /><br />
    <div id="bar" style="width:156px; border:#000000 2px solid; padding:1px; visibility:hidden;">
        <span id="bar1" style="background-color:#FFFFFF">   </span>
        <span id="bar2" style="background-color:#FFFFFF">   </span>
        <span id="bar3" style="background-color:#FFFFFF">   </span>
        <span id="bar4" style="background-color:#FFFFFF">   </span>
        <span id="bar5" style="background-color:#FFFFFF">   </span>
        <span id="bar6" style="background-color:#FFFFFF">   </span>
        <span id="bar7" style="background-color:#FFFFFF">   </span>
        <span id="bar8" style="background-color:#FFFFFF">   </span>
        <span id="bar9" style="background-color:#FFFFFF">   </span>
        <span id="bar10" style="background-color:#FFFFFF">   </span>
        
    </div>
    <br />
    <div id="result"></div>
</body>
</html>

progressBar.js:

程式碼var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function doStart() {
    init();
    document.getElementById("bar").style.visibility = "visible";
}

function init() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleChange;
    var url = "progressBar.mgc?action=init×tamp=" + new Date().getTime();
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function handleChange() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            refresh();
        }
    }
}

function refresh() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = callback;
    var url = "progressBar.mgc?action=run×tamp=" + new Date().getTime();
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    setTimeout("refresh()", "500");
}

function callback() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            parseResult();
        }
    }
}

function parseResult() {
    var xmlDoc = xmlHttp.responseXML;
    var pros = xmlDoc.getElementsByTagName("progress");
    var result = "";
    for (var i = 0; i < pros.length; i++) {
        var massage = pros[i].childNodes[0].childNodes[0].nodeValue;
        var percent = pros[i].childNodes[1].childNodes[0].nodeValue;
        result += massage + "[" + percent + "%]" + "<br />";
    }
    document.getElementById("result").innerHTML = result;
    clearBar();
    var percents = xmlDoc.getElementsByTagName("percent");
    var nowPercent = percents[percents.length - 1].childNodes[0].nodeValue;
    var count = parseInt(nowPercent / 10);
    for (var i = 0; i <= count; i++) {
        if (i != 0) {
            var bar = document.getElementById("bar" + i);
            bar.style.backgroundColor = "#bababa";
        }
    }
}

function clearBar() {
    for (var i = 1; i <= 10; i++) {
        var bar = document.getElementById("bar" + i);
        bar.style.backgroundColor = "#FFFFFF";
    }
}

progressBar.java:

程式碼package cn.edu.ahau.mgc.ajax.progressbar;

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;

public class ProgressBar extends HttpServlet {
    
    private int count;
    private StringBuffer str;
    public static final int TOTAL = 25;

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

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("init".equals(action)) {
            this.count = 1;
            this.str = new StringBuffer();
        } else if ("run".equals(action)) {
            if (this.count <= TOTAL) {
                int percent = count * 100 / TOTAL;
                str.append("<progress><massage> 完成了 " + (this.count++) + "個任務</massage><percent>" + percent + "</percent></progress>");
            }
            response.setContentType("text/xml; charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            PrintWriter out = response.getWriter();
            out.print("<response>" + str + "</response>");
            out.flush();
            out.close();
        }
    }

}

web.xml:

程式碼<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ProgressBar</servlet-name>
    <servlet-class>cn.edu.ahau.mgc.ajax.progressbar.ProgressBar</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ProgressBar</servlet-name>
    <url-pattern>/progressBar.mgc</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.