0
好的,這是我正在做的。檢查出的PHP和Java腳本下面的腳本一個ajax請求,多重響應?
PHP
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient("server/test.wsdl");
$origtext = array('user'=>$_GET['pointname']);
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = $client->getTime($origtext)->{"timest"};
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
// sleep(1);
usleep(10000); // sleep 10ms to unload the CPU
clearstatcache();
$currentmodif = $client->getTime($origtext)->{"timest"};
}
$response = array();
$response['msg'] = $client->getMobile($origtext)->{"phone-num"};
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
的Javascript
var timestamp = 0;
var pointname = "test1";
var noerror = true;
function wait() {
$.ajax({
type: "GET",
url: "backend.php",
data: "timestamp=" + this.timestamp + "&pointname=" + pointname,
success: function (transp) {
var response = eval('(' + transp + ')');
timestamp = response['timestamp'];
$("#page1b").html(response['msg']);
$("#page1c").html(timestamp);
noerror = true;
},
complete: function (transp) {
if (!noerror) setTimeout(function() {
wait()
}, 5000);
else wait();
noerror = false;
}
});
}
wait();
一次Ajax請求被激發,從服務器端會做一個while循環檢查時間值直到時間改變,然後它會回顯一個新的值返回給客戶端並顯示在網頁上(這一切都將自動進行,並且c上的內存消耗也不會太多我的最終目標是建立一個內部有100個值的表,發射100個Ajax請求顯然是不可能的。我想分別處理這些值,但所有共享相同的PHP腳本,這意味着每個值只會改變,而不會受到其他人的影響,但只有一個PHP腳本需要顯示。有什麼建議?
它工作嗎?我不是一個PHP的人,但我會認爲PHP代碼會產生一個只需要很長時間的單一響應。 – nnnnnn
你應該傳遞一個回調函數來運行'complete',而不是用這個'wait()'東西。當您正確使用它時,函數式編程可以非常優雅。 – meagar