保持jQuery .getJSON()连接打开并在页面正文中等待? [英] Keeping a jQuery .getJSON() connection open and waiting while in body of a page?

查看:91
本文介绍了保持jQuery .getJSON()连接打开并在页面正文中等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

这很好用,即使是服务器返回null(它会超过2分钟之后)。它会不断地无限期地调用getJSON函数。快乐熊猫。

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.

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

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>

这有效......暂时不久,它会停止调用check4Updates并获取进入无限循环并使用100%的处理器时间。

This works... for a while. Shortly thereafter, it will stop calling check4Updates and get into an infinite loop and use 100% processor time.

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

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.

最好的问候,
Van Nguyen

Best Regards, Van Nguyen

推荐答案

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

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:

来源: 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);

这篇关于保持jQuery .getJSON()连接打开并在页面正文中等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆