admin管理员组文章数量:1316329
I'm writing a basic push messaging system. A small change has caused it to stop workingly properly. Let me explain. In my original version, I was able to put the code in the of a document something like this:
<head>
...text/javascript">
$(document).ready(function(){
$(document).ajaxStop(check4Updates);
check4Updates();
});
function check4Updates(){
$.getJSON('%PATH%', callback);
};
...
</head>
This worked nicely and would keep the connection open even if the server return null (which it would after say a 2 minute timeout). It would just keep calling the getJSON function over and over indefinitely. Happy Panda.
Now, I must put the code segment in between tags. Access to the $(document).ready() function pretty much won't work.
<body>
...
check4Updates();
$("body").ajaxStop(check4Updates);
...
</body>
This works... for a while. Shortly thereafter, it will stop calling check4Updates and get into an infinite loop and use 100% processor time.
I'm trying to get it so that check4Updates is repeatedly called until the page is closed. If anyone has any insights as to why my simple change is no longer functioning as expected, PLEASE let me know. Thank you for taking the time to read and help me out.
Best Regards, Van Nguyen
I'm writing a basic push messaging system. A small change has caused it to stop workingly properly. Let me explain. In my original version, I was able to put the code in the of a document something like this:
<head>
...text/javascript">
$(document).ready(function(){
$(document).ajaxStop(check4Updates);
check4Updates();
});
function check4Updates(){
$.getJSON('%PATH%', callback);
};
...
</head>
This worked nicely and would keep the connection open even if the server return null (which it would after say a 2 minute timeout). It would just keep calling the getJSON function over and over indefinitely. Happy Panda.
Now, I must put the code segment in between tags. Access to the $(document).ready() function pretty much won't work.
<body>
...
check4Updates();
$("body").ajaxStop(check4Updates);
...
</body>
This works... for a while. Shortly thereafter, it will stop calling check4Updates and get into an infinite loop and use 100% processor time.
I'm trying to get it so that check4Updates is repeatedly called until the page is closed. If anyone has any insights as to why my simple change is no longer functioning as expected, PLEASE let me know. Thank you for taking the time to read and help me out.
Best Regards, Van Nguyen
Share Improve this question asked Apr 30, 2009 at 22:29 Van NguyenVan Nguyen 4,0692 gold badges27 silver badges17 bronze badges4 Answers
Reset to default 7Yeah, you're not going to want to use that loop, you're pretty much DOSing yourself not to mention locking the client.
Simple enough, create a polling plugin:
Source: http://web.archive/web/20081227121015/http://buntin:80/2008/sep/23/jquery-polling-plugin/
Usage:
$("#chat").poll({
url: "/chat/ajax/1/messages/",
interval: 3000,
type: "GET",
success: function(data){
$("#chat").append(data);
}
});
The code to back it up:
(function($) {
$.fn.poll = function(options){
var $this = $(this);
// extend our default options with those provided
var opts = $.extend({}, $.fn.poll.defaults, options);
setInterval(update, opts.interval);
// method used to update element html
function update(){
$.ajax({
type: opts.type,
url: opts.url,
success: opts.success
});
};
};
// default options
$.fn.poll.defaults = {
type: "POST",
url: ".",
success: '',
interval: 2000
};
})(jQuery);
Use a timeout, these types of loops are a really bad idea, so if you must employ polling for whatever reason, make sure you don't keep hammering your backend.
You either need to change to a polling method as described by altCognito, or you can use et, which is designed to push data from the server to the client. Depending on your needs, you could use something like Jetty (for Java) or Twisted (Python) or WebSync (ASP.NET/IIS)
I think that this is about server push?
How about using the Socket.IO?
Ref: Introducing Socket IO 0.9, Google Group (http://socket.io/)
Ref: http://techoctave./c7/posts/60-simple-long-polling-example-with-javascript-and-jquery
Ref: Fun with websockets, Node.js and jQuery (http://nayna/blog/?p=159)
版权声明:本文标题:javascript - Keeping a jQuery .getJSON() connection open and waiting while in body of a page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742004245a2411637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论