需要彗星计划的解释
我是彗星编程的新手。我搜索并编写了这样的代码,
<script type="text/javascript" charset="utf-8">
function waitForMsg(){
$.ajax({
type: "GET",
url: "getMessage.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
$('#messages).append(data);
setTimeout(
'waitForMsg()',
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
'waitForMsg()',
"15000");
},
});
};
$(document).ready(function(){
waitForMsg();
});
</script>
当我将消息传递给 getMessage.php 时,我会从 getMessage.php 获取更新,
但我怀疑我使用了
setTimeout('waitForMsg()',1000);
这意味着什么。如果我们每 1 秒调用一次 waitForMsg() ,那么 Ajax 和 Comet 编程之间有什么区别。
I am new to Comet programming. I searched through and write the code like this
<script type="text/javascript" charset="utf-8">
function waitForMsg(){
$.ajax({
type: "GET",
url: "getMessage.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
$('#messages).append(data);
setTimeout(
'waitForMsg()',
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
'waitForMsg()',
"15000");
},
});
};
$(document).ready(function(){
waitForMsg();
});
</script>
I am getting update from getMessage.php when ever i am passing a message to getMessage.php
But my doubt is i used
setTimeout('waitForMsg()',1000);
What this means. If we are calling waitForMsg() for every 1 sec then what is the difference between Ajax and Comet programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的示例,Ajax 和 Comet 之间的区别取决于 getMessage.php 如何响应客户端的请求。
使用Ajax,即使没有新消息返回,getMessage.php也会立即返回。然后,客户端将等待指定的时间间隔,然后发送另一个请求,要么再次收到新消息,要么什么也不接收。最终结果是客户端更新新消息的最快速度是指定的时间间隔。
另一方面,使用 Comet,getMessage.php 在有内容要返回之前不会返回。这意味着根据客户端的初始请求,它将检查是否有新消息,如果没有可用的消息,它将休眠一段时间并再次检查。此过程将继续,直到有新消息可用或发生超时,此时客户端将向服务器发送另一个请求,并且该过程重新开始。在这种情况下,最终结果是客户端看起来会立即更新。
Regarding your example, the difference between Ajax and Comet depends on how getMessage.php responds to the request from the client.
Using Ajax, getMessage.php would return immediately even if there are no new messages to return. The client would then wait the specified interval and then send another request, again either receiving a new message or nothing at all. End result is that the quickest the client will ever update with a new message is the specified interval.
Using Comet, on the other hand, getMessage.php would not return until it has something to return. Meaning on initial request from the client it would check for new messages, if none are available it would sleep for a certain amount of time and check again. This process would continue until a new message is available or a timeout occurs, at which point the client would send another request to the server and the process starts over. End result in this case is that the client would appear to update instantaneously.
COMET 有多种类型。您正在使用AJAX轮询,但还有其他技术,例如永远-frame,不涉及轮询。
Forever-frame 使用服务器推送的隐藏无限长 iframe
script
根据需要标记。另请注意,AJAX 轮询的某些用途(例如,维基百科所解释的)不包含
setTimeout
,因为它们会立即在成功处理程序中启动新的 AJAX 请求。另外(使用 AJAX 轮询示例),您可以只使用:
不需要字符串。
There are multiple kinds of COMET. You're using AJAX polling, but there are other techniques, like forever-frame, that do not involve polling.
Forever-frame uses a hidden infinitely long iframe that the server pushes
script
tags to as needed.Also note that some uses of AJAX polling (e.g. as explained by the Wikipedia) do not include a
setTimeout
, since they immediately start a new AJAX request in the success handler.Also (with the AJAX polling example), you can just use:
There is no need for a string.