PHP + JavaScript to dynamically display the server running progress bar (cool)

Source: Internet
Author: User

I have a PHP program that needs to call XMLRPC cyclically for about 500 times and run for more than 20 minutes. When the program runs, the client only has a blank page, and the browser status is always load. As a user, this kind of waiting is long, and there is more complaints.
Solution
If a progress bar is displayed in front of the user and tells the user what the server is doing, the problem can be basically solved, although the running time on the server is not reduced, however, there will be subtle changes in the user's mind, and it is no longer boring to wait. At least there is a hope to wait :).
Considering that each call to XMLRPC is time-consuming, you can use the flush () function of PHP to output a piece of JavaScript to dynamically update the progress bar display in the client browser.
Code annotation
HTML code of the progress bar:

Code:

Px ">
The dynamic effect of the following progress bar is generated by the server-side PHP program combined with the client-side JavaScript program.
Px ">

 

0%


Use the background color of the div as the progress bar.
Client JavaScript function updateProgress (sMsg, iWidth), used to update the progress bar
Code:



The sMsg parameter is the status text displayed in the status, while the iWidth parameter is the length of the current progress bar. This value and the maximum length of the progress bar $ width are calculated by the server.
Each time a time-consuming operation is called, the PHP flush () function is used to output a piece of JavaScript code:
Code:




Code:

If ('source' = $ _ GET ['act ']) {// view the source code
Show_source ($ _ SERVER ["SCRIPT_FILENAME"]);
Exit;
} // End if
Set_time_limit (0 );
For ($ I = 0; I I <500; $ I ++ ){
$ Users [] = 'Tom _ '. $ I;
} // End
$ Width = 500; // The length of the displayed progress bar, in px
$ Total = count ($ users); // total number of records to be operated
$ Pix = $ width/$ total; // The length of the progress bar for each operation record
$ Progress = 0; // The length of the current progress bar
?>
Http://www.w3.org/TR/xhtml1/DTD/transitional.dtd>


 Dynamically display the progress bar for running programs on the server
 
 
 
 



Px ">
The dynamic effect of the following progress bar is generated by the server-side PHP program combined with the client-side JavaScript program.
Px ">

 

0%

Flush (); // send the output to the client browser
Foreach ($ users as $ user ){
// Use an empty loop here to simulate time-consuming operations. replace them in actual applications;
// If your operation is not time-consuming, I think you do not need to use this script :)
For ($ I = 0; I I <10000; $ I ++ ){
;;
}
?>

Flush (); // send the output to the client browser so that it can immediately execute the JavaScript program output by the server.
$ Progress + = $ pix;
} // End foreach
// Finally, set the progress bar to the maximum value $ width, and the operation is complete.
?>

Flush ();
?>




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.