下面的代码是否递归使用内存 [英] Is the code below using memory recursively

查看:73
本文介绍了下面的代码是否递归使用内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JavaScript做一个非常类似Comet的长时间轮询 下面的代码似乎有效,但是是否递归地调用了自己并占用了资源?我怎么知道?

I'm doing a very simple Comet-like long polling in JavaScript The code below seems to work, but is it recursively calling itself and eating resources? How do I tell ?

编辑:根据Dogberts建议编辑的代码

Code edited as per Dogberts suggestion

$(document).ready(function () {
    function doSuccess(data) {
        $('#res').html(data.time + "<br/>" + data.data);
        startAjax();
    }
    function doError(jqXHR, textStatus, errorThrown) {
        $('#res').html(textStatus + ":" + errorThrown);
        startAjax();
    }
    function startAjax() {
        $.ajax({
            url: 'http://127.0.0.1:12345/',
            dataType: 'jsonp',
            success: doSuccess,  //Edit: updated as per Dogbert's suggestion
            error: doError
        });
    }
    startAjax();
});

我已经针对它运行 http://home.orange.nl/jsrosman/在那里(我只是出于专业上的偏执)看起来还可以,startAjax调用(以及调用返回)到doSuccess,后者调用了startAjax

I've run http://home.orange.nl/jsrosman/ against it and it looks to be OK there (I'm just being professionally paranoid) startAjax calls (well callsBack) to doSuccess which calls startAjax

推荐答案

不,这不应该消耗任何额外的资源.旧请求完成后,您可以正确地调用该方法.

No, this shouldn't be eating any extra resources. You're properly calling the method after the old request has been completed.

在旁注,

        success: function (data) {
            doSuccess(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            doError(jqXHR, textStatus, errorThrown);
        }

可以写为

success: doSuccess,
error: doError

这篇关于下面的代码是否递归使用内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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