保持jQuery .getJSON()连接打开并在页面正文中等待?

时间:2021-11-26 12:50:44

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.

这很好地工作,并且即使服务器返回null(它会在超时2分钟之后),也会保持连接打开。它会无限期地一遍又一遍地调用getJSON函数。快乐的熊猫。

Now, I must put the code segment in between tags. Access to the $(document).ready() function pretty much won't work.

现在,我必须将代码段放在标签之间。几乎无法访问$(document).ready()函数。

<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.

这有效......有一段时间了。此后不久,它将停止调用check4Updates并进入无限循环并使用100%处理器时间。

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.

我试图得到它,以便重复调用check4Updates直到页面关闭。如果有人对我为什么简单的更改不再按预期运行有任何见解,请告诉我。感谢您抽出宝贵时间阅读并帮助我。

Best Regards, Van Nguyen

最诚挚的问候,Van Nguyen

4 个解决方案

#1


Yeah, 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.org/web/20081227121015/http://buntin.org: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);

#2


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.

使用超时,这些类型的循环是一个非常糟糕的主意,所以如果你因任何原因必须使用轮询,请确保你不要继续锤击你的后端。

#3


You either need to change to a polling method as described by altCognito, or you can use comet, 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)

您需要更改为altCognito描述的轮询方法,或者您可以使用comet,它旨在将数据从服务器推送到客户端。根据您的需要,您可以使用Jetty(用于Java)或Twisted(Python)或WebSync(ASP.NET / IIS)之类的东西

#4


I think that this is about server push?

我认为这是关于服务器推送?

How about using the Socket.IO?

使用Socket.IO怎么样?

Ref: Introducing Socket IO 0.9, Google Group (http://socket.io/)

参考:介绍Socket IO 0.9,Google Group(http://socket.io/)

Ref: http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Ref: Fun with websockets, Node.js and jQuery (http://nayna.org/blog/?p=159)

参考:有关websockets,Node.js和jQuery的乐趣(http://nayna.org/blog/?p=159)

#1


Yeah, 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.org/web/20081227121015/http://buntin.org: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);

#2


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.

使用超时,这些类型的循环是一个非常糟糕的主意,所以如果你因任何原因必须使用轮询,请确保你不要继续锤击你的后端。

#3


You either need to change to a polling method as described by altCognito, or you can use comet, 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)

您需要更改为altCognito描述的轮询方法,或者您可以使用comet,它旨在将数据从服务器推送到客户端。根据您的需要,您可以使用Jetty(用于Java)或Twisted(Python)或WebSync(ASP.NET / IIS)之类的东西

#4


I think that this is about server push?

我认为这是关于服务器推送?

How about using the Socket.IO?

使用Socket.IO怎么样?

Ref: Introducing Socket IO 0.9, Google Group (http://socket.io/)

参考:介绍Socket IO 0.9,Google Group(http://socket.io/)

Ref: http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Ref: Fun with websockets, Node.js and jQuery (http://nayna.org/blog/?p=159)

参考:有关websockets,Node.js和jQuery的乐趣(http://nayna.org/blog/?p=159)