爲了避免我的服務器上多要求,我想實現一個池。所以我做了要求,一旦給我的數據,如果它發現它每隔30秒,或者30秒過去了!
//getchat.php
$pool=0;
$chatmsg= array();
while($pool<30 && count($chatmsg)==0){
$result = mysqli_query($con,"SELECT * FROM chat WHERE id > ".addslashes($_GET['id'])." ORDER BY id ASC");
$i=0;
while($row = mysqli_fetch_assoc($result)) {
$chatmsg[$i]=array("message"=>htmlspecialchars_decode(htmlentities($row["message"])));
$i++;
}
sleep(1);
$pool++;
}
echo json_encode($chatmsg);
flush();
//script.js
setInterval(function() {
$.get("getchat.php", {
id: chatid
}, function(data) {
console.log(data);
});
}, 30000);
但消息顯示後30個secondes我在做什麼錯
編輯:我終於發現:
秒後0function getchat(){
$.get("getchat.php", {
id: chatid
}, function(data) {
console.log(data);
getchat();
});
}
如果您從瀏覽器中調用「getchat.php?id = someid」,會發生什麼?它實際上是否返回任何json? –
是的!我擁有所有的json – lopata
也許你應該考慮web套接字,而不是像你的服務器端代碼那樣使用'sleep()'。 –