我的页面上有一个很长的轮询请求.服务器端的脚本在20秒后设置为超时.
因此,当长轮询"空闲"并且用户按下另一个按钮时,该新请求的发送被延迟,直到前一个脚本超时.
我看不出jQuery方面的代码有什么问题.为什么onclick事件会延迟?
function poll()
{
$.ajax({
url: "/xhr/poll/1",
data: {
user_id: app.user.id
},
type: "POST",
dataType: "JSON",
success: pollComplete,
error: function(response) {
console.log(response);
}
});
}
function pollComplete()
{
poll();
}
function joinRoom(user_id)
{
$.ajax({
url: "/xhr/room/join",
dataType: "JSON",
type: "POST",
data: {
user_id: app.user.id,
room_id: room.id
}
});
}
<button id="join" onclick="javascript:joinRoom(2);">Join</button>
############ PHP Controller on /xhr/poll
$time = time();
while ((time() - $time) < 20)
{
$updates = $db->getNewStuff();
foreach ($updates->getResult() as $update)
$response[] = $update->getResponse();
if …
Run Code Online (Sandbox Code Playgroud)