萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> 互動設計

ASP 進度條實現:Response.Buffer 與 Response.Flush 的流式輸出技巧

利用 ASP 內建緩衝機制,通過 `Response.Flush` 分批輸出 HTML 片段,在長時間後台處理過程中即時向瀏覽器推送進度更新,實現無組件的服務端進度條效果。

核心原理

  • 開啟 `Response.Buffer = true`,頁面內容先存入伺服器緩衝區。
  • 主程序循環調用 `delayshow(dstep, dmax)`,根據已處理步數計算完成百分比。
  • 每達到 1% 閾值,輸出一個圖片標籤(如 ``)並 `Response.Flush` 立即推送給瀏覽器。
  • 變量 `lostnum` 累積未達 1% 的碎片步數,`shownum` 記錄已輸出總數,最後 `showlost()` 補齊剩餘進度。

關鍵代碼

response.buffer = true
dim lostnum, shownum
delayshow(dstep, dmax)

sub delayshow(dstep, dmax)
    dim total, dtemp
    total = 100
    dtemp = dstep
    dstep = dstep + lostnum
    if (dstep * total) / dmax < 1 then
        lostnum = lostnum + dtemp
    else
        for ccc = 1 to fix((dstep * total) / dmax)
            Response.Flush
            shownum = shownum + fix((dstep * total) / dmax)
            lostnum = 0
        next
    end if
end sub

sub showlost()
    for ccc = 1 to (100 - shownum)
        Response.Write("<img src='bar.gif'>")
    next
    Response.Write "<br>"
    Response.Flush
end sub
註:更多精彩教程請關注三聯圖文教程頻道。
copyright © 萬盛學電腦網 all rights reserved