2012-12-07 32 views
0

我只是獲得第一次的消息,然後代碼停止工作。爲什麼這個長時間的投票不起作用?

我剛剛學習長時間投票(我知道,那裏有很多錯誤和愚蠢也).Plz的幫助。

jquery的功能就在這裏。

var timestamp = null; 

var last_msg_id = 1; 
    function chat(id, name) { 

$('#chatcom').show('fast'); 
$.ajax({ 
    type:"Post", 

    url:"load.php", 
    data:{ 
     timestamp:timestamp 
    }, 
    dataType:"json", 

    async:true, 
    cache:false, 
    success:function(data) { 

     var json = data; 
     $("#chatcom").html(json['msg']); 

     last_msg_id = json['last_msg_id_db']; 

     timestamp = json["timestamp"]; 
    }, 
    error:function(XMLhttprequest, textstatus, errorthrown) { 
     alert("error:" + textstatus + "(" + errorthrown + ")"); 
    } 




}); 

}

load.php在這裏

$filename='data.php'; 

$lastmodif=isset($_POST['timestamp'])?$_POST['timestamp']:0; 
$currentmodif=filemtime($filename); 
$last_msg_id=$_POST['last_msg_id']; 
$session=new $session; 

$sql=mysqli_query($db3->connection,"SELECT * FROM chat_com where id>'$last_msg_id' ORDER by id ASC"); 

$sql_m=mysqli_query($db3->connection,"SELECT max(id) as maxid FROM chat_com"); 
$row_m=mysqli_fetch_array($sql_m); 
$last_msg_id_db=$row_m['maxid']; 
$final_response=array(); 
if($last_msg_id_db>$last_msg_id){ 

    while($row=mysqli_fetch_array($sql)){ 
     $text=$row['mesg']; 
     $fro=$row['fro']; 
     $tocomr=$row['tocom']; 

     $handle = fopen('data.php', 'a'); 
     fwrite($handle, $text); 
     fclose($handle); 

    } 
} 

while($currentmodif<=$lastmodif){ 
    usleep(10000); 
    clearstatcache(); 
    $currentmodif=filemtime($filename); 
} 

$response=array(); 
$response['msg']=file_get_contents($filename); 
$response['last_msg_id_db']=$last_msg_id_db; 

$response['timestamp']=$currentmodif; 
echo json_encode($response); 
+0

你得到的錯誤是什麼? 「_我只是第一次收到message_」**是這個默認消息**或者只是第一條消息發送**? – Jai

+1

如果'chat()'被調用一次,那麼它會響應一次。對於重複的長輪詢,該功能需要重複調​​用。這通常會從ajax成功處理程序的最後一行完成。 –

回答

0

,你必須再次撥打您的功能,所以設置了超時功能,在年底成功子句中,

success: function(data) { 
     // your code.... 
setTimeout('yourfunction()',1000); // in your case it will be chat() function 
}