【发布时间】:2021-04-12 16:52:43
【问题描述】:
所以基本上我的 ajax 函数一直在刷新,我希望它只是在检测到数据库更改时刷新。有没有办法做到这一点?谢谢
我的密码:
function ajax(){
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if (req.readyState == XMLHttpRequest.DONE && req.status == 200) {
document.getElementById('chat').innerHTML = req.responseText;
}
}
req.open('GET', 'chat.php', true);
req.send();
return false;
}
setInterval(function(){ajax();}, 100);
【问题讨论】:
-
单独使用 ajax 没有。研究使用 websockets 或服务器事件 API 的
标签: javascript xml xmlhttprequest