2011-11-08 77 views
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脚本需要显示。有什么建议?

+0

它工作吗?我不是一个PHP的人,但我会认为PHP代码会产生一个只需要很长时间的单一响应。 – nnnnnn

+0

你应该传递一个回调函数来运行'complete',而不是用这个'wait()'东西。当您正确使用它时,函数式编程可以非常优雅。 – meagar

回答

1

不,你不能。在较旧的浏览器中,这是可能的,并且甚至有一个JQuery plug-in来促进它,但是较新的浏览器仅在收到整个请求时触发事件,并且不允许您在此之前读取部分响应。