php+javascript實現的動態顯示服務器運行程序進度條功能示例

本文實例講述了php+javascript實現的動態顯示服務器運行程序進度條功能。分享給你們供你們參考,具體以下:javascript

常常有這樣的業務要處理,服務器上有較多的業務須要處理,須要分批操做,因而就須要一個提示客戶如今完成進度的進度條。php

這個是php+javascript的進度條。html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
//set_time_limit(0);    //注意,若是是安全模式,請不要打開,若是不是安全模式,這個選項能夠打開
for ( $i = 0; $i < 500; $i ++) {
   $users [] = 'Tom_' . $i ;
//end for
$width = 500;            //顯示的進度條長度,單位 px
$total = count ( $users );       //總共須要操做的記錄數
$pix = $width / $total ;       //每條記錄的操做所佔的進度條單位長度
$progress = 0;           //當前進度條長度
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd" >
<html>
<head>
   <title>動態顯示服務器運行程序的進度條</title>
   <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
   <style>
   body, div input { font-family: Tahoma; font-size: 9pt }
   </style>
   <script language= "JavaScript" >
   <!--
   function updateProgress(sMsg, iWidth)
   {
     document.getElementById( "status" ).innerHTML = sMsg;
     document.getElementById( "progress" ).style.width = iWidth + "px" ;
     document.getElementById( "percent" ).innerHTML = parseInt(iWidth / <?php echo $width ; ?> * 100) + "%" ;
    }
   //-->
   </script>
</head>
<body>
<div style= "margin: 4px; padding: 8px; border: 1px solid gray; background: #EAEAEA; width: <?php echo $width+8; ?>px" >
   <div><font color= "gray" >以下進度條的動態效果由服務器端 PHP 程序結合客戶端 JavaScript 程序生成。</font></div>
   <div style= "padding: 0; border: 1px solid navy; width: <?php echo $width; ?>px" >
   <div id= "progress" style= "padding: 0; border: 0; width: 0px; text-align: center;  height: 16px" ></div>
   </div>
   <div id= "status" > </div>
   <div id= "percent" style= "position: relative; top: -30px; text-align: center; font-weight: bold; font-size: 8pt" >0%</div>
</div>
<?php
flush ();  //將輸出發送給客戶端瀏覽器
foreach ( $users as $user ) {
   //   在此處使用空循環模擬較爲耗時的操做,實際應用中需將其替換;
   //   若是你的操做不耗時,我想你就不必使用這個腳本了 :)
   //   請在這裏處理你的業務
   for ( $i = 0; $i < 1000000; $i ++) {
     ;;
    }
?>
<script language= "JavaScript" >
   updateProgress( "正在操做用戶「<?php echo $user; ?>」 ...." , <?php echo min( $width , intval ( $progress )); ?>);
</script>
<?php
   flush ();  //將輸出發送給客戶端瀏覽器,使其能夠當即執行服務器端輸出的 JavaScript 程序。
   $progress += $pix ;
//end foreach
//  最後將進度條設置成最大值 $width,同時顯示操做完成
?>
<script language= "JavaScript" >
   updateProgress( "操做完成!" , <?php echo $width ; ?>);
</script>
<?php
flush ();
?>
</body>
</html>

運行效果以下:java

相關文章
相關標籤/搜索