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