使用 jQuery 和 PHP 进行长轮询
因此,我一直在尝试使用 jQuery 库和 PHP 进行长轮询。我这样做是为了将来可以制作某种实时通知系统。我现在拥有的代码并不能真正工作。
索引.php
<html>
<head>
<title>Long Polling</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
getData();
});
function getData() {
$.ajax({
type: "POST",
url: "ajax.php",
async: true,
timeout: 50000,
data: "get=true",
success: function(data) {
$("#info").append(data);
setTimeout("getData()", 1000);
}
});
}
</script>
</head>
<body>
<div id='info'></div>
</body>
</html>
Ajax.php
<?php
if(rand(1, 100) % 2) {
echo 'even';
} else {
sleep(rand(1, 4));
}
?>
So, I've been trying to do Long-Polling using the jQuery Library and PHP. I'm doing this so I can make some sort of real-time notifications system in the future. The code I have now isn't really working.
index.php
<html>
<head>
<title>Long Polling</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
getData();
});
function getData() {
$.ajax({
type: "POST",
url: "ajax.php",
async: true,
timeout: 50000,
data: "get=true",
success: function(data) {
$("#info").append(data);
setTimeout("getData()", 1000);
}
});
}
</script>
</head>
<body>
<div id='info'></div>
</body>
</html>
Ajax.php
<?php
if(rand(1, 100) % 2) {
echo 'even';
} else {
sleep(rand(1, 4));
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其用于 ajax.php
观看此,有时您必须等待 12 点 如此
如果你让他在一秒内完成,它看起来会被破坏,但事实并非
Try to use this for ajax.php
watch this and sometimes you have to wait up to 12 seconds
if you let him to complete in one second it appears to be broken, but it's not