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

发布于 2024-07-18 14:56:54 字数 795 浏览 5 评论 0 原文

我正在编写一个基本的推送消息系统。 一个小小的变化导致它停止正常工作。 让我解释。 在我的原始版本中,我能够将代码放入文档中,如下所示:

<head>
  ...text/javascript">
  $(document).ready(function(){
    $(document).ajaxStop(check4Updates);
    check4Updates();
  });

  function check4Updates(){
    $.getJSON('%PATH%', callback);
  };
...
</head>

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

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

<body>
...
check4Updates();
$("body").ajaxStop(check4Updates);
...
</body>

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

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

此致, 范阮

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-07-25 14:56:54

是的,你不会想使用这个循环,你几乎DOSing 自己更不用说锁定客户端了。

很简单,创建一个轮询插件:

来源:http://web.archive.org/web/20081227121015/http://buntin.org:80/2008/sep/23/jquery-polling-plugin/

用法:

$("#chat").poll({
    url: "/chat/ajax/1/messages/",
    interval: 3000,
    type: "GET",
    success: function(data){
        $("#chat").append(data);
    }
});

备份的代码:

(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);

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);
浮华 2024-07-25 14:56:54

使用超时,这些类型的循环是一个非常糟糕的主意,因此,如果您出于某种原因必须使用轮询,请确保不要继续敲击后端。

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.

时间你老了 2024-07-25 14:56:54

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

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)

情话已封尘 2024-07-25 14:56:54

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

使用 Socket.IO 怎么样?

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

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

参考:Websockets、Node.js 和 jQuery 的乐趣 (http://nayna.org/blog/?p=159)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文