我正在嘗試獲得一個進度條,顯示一個長程序的實時進度。我想我已經完成了大部分作業,但我遇到了一個有趣的路障。
簡而言之,我有一個按鈕,在啟動時會呼叫一個 JavaScript 函式,該函式將做兩件事:
- 啟動異步 Ajax 呼叫以啟動我長時間運行的腳本。該腳本將使用某些關鍵代碼塊的進度更新表格,因此表格將具有從 0 到 100 的數字和一些訊息。
- 在計時器中啟動同步呼叫以讀取資料庫表以獲取為用戶更新進度條的進度
當我啟動它時,我注意到(2)將等待(1)。我注意到呼叫已發送(在 DeveloperTools -> Debug 中),但(我相信)CodeIgniter 將第二個 Ajax 呼叫排隊,直到第一個呼叫完成。
有沒有辦法解決這個問題,以便我的 (2) 呼叫在 (1) 仍然執行時多次轉到 DB 并回傳?
只是放一些代碼:
function button_pressed_for_long_action(type, id)
{
//start the timer
timer = window.setInterval(get_progress, 3000);
//call the long script");
$.ajax({
url: "/the URL for long action/",
dataType: "json",
method: "POST",
data: {
type : type,
id : id
},
success:function(data)
{
},
error: function( data, status, error ) {
alert("error");
alert(error);
}
});
}
以及計時器呼叫的函式以獲取進度:
function get_progress()
{
$.ajax({
url: "/url to get process/",
dataType: "json",
method: "POST",
async : false,
data: {
some_id : some_id
},
success:function(data)
{
//update UI
if (progress < 100)
{
//exit if not done
return;
}
//script is finished
window.clearInterval(timer);
},
error: function( data, status, error ) {
alert("error");
alert(error);
}
});
}
URL 呼叫 CodeIgniter 控制器函式,這些函式讀取 DB 并正確回傳帶有資訊的 JSON。
問題只是獲得進度 (2) 等到 (1) 完成。
提前致謝!
uj5u.com熱心網友回復:
經過一番挖掘后,我找到了它。PHP 似乎像我想的那樣排隊請求。佇列是每個會話的。因此,如果會話關閉,則解除鎖定并且可以通過另一個呼叫。
所以在我的情況下,我需要在長執行腳本的開頭有以下行:
The_long_script_controller.php
function execute_long_script()
{
//this is the line I needed:
session_write_close();
//then do the full long processing
}
全部基于此執行緒: 兩個同時的 AJAX 請求不會并行運行
這解決了我的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/513754.html
