Ajax Basics Tutorial (4)-Implement basic AJAX technology 4.5 show progress bar

Source: Internet
Author: User
Tags visibility


Without exception, almost every application will invoke a long-running transaction at a time. If you are concerned about the usability of the system, make sure that users can easily see the state of the system. If it's a fat customer application, the solution to the long-running transaction problem is simple: just display a progress bar so that users know what is happening. However, it is not easy to do this in a Web application before Ajax. This section will give you an idea of how to use Ajax to create a progress bar for Web applications.



In the example shown in Listing 4-9, the settimeout () is again used in the Pollcallback () method to invoke the server every 2 seconds. In the Processresult () method, only the first digit of the completed scale (percentage) is found from the server, which shows which progress blocks in the progress bar are shaded (gray).



Code Listings 4-9 progressbar.html


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<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 () {
if (window. ActiveXObject) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();
}
}
function Go () {
Createxmlhttprequest ();
Checkdiv ();
var url = "Progressbarservlet?task=create";
var button = document.getElementById ("Go");
Button.disabled = true;
Xmlhttp.open ("Get", url, True);
Xmlhttp.onreadystatechange = Gocallback;
Xmlhttp.send (NULL);
}
function Gocallback () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
SetTimeout ("Pollserver ()", 2000);
}
}
}
function Pollserver () {
Createxmlhttprequest ();
var url = "progressbarservlet?task=poll&key=" + key;
Xmlhttp.open ("Get", url, True);
Xmlhttp.onreadystatechange = Pollcallback;
Xmlhttp.send (NULL);
}
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 ()", 2000);
} 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 < i++) {
var elem = document.getElementById ("block" + i);
elem.innerhtml = clear;
Elem.style.backgroundColor = "White";
}
}
</script>
<body>
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" >&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>
&LT;TR&GT;&LT;TD align= "center" id= "complete" ></td></tr>
</tbody>
</table>
</body>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.