Is the code below using memory recursively

I am doing a very simple comet long poll in JavaScript. It seems that the code below works, but does it call itself recursively and feed on resources? How can i say

EDIT: Code edited as suggested by Dogberts

$(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 suggestion
            error: doError
        });
    }
    startAjax();
});

I ran http://home.orange.nl/jsrosman/ against it and it looks like everything is fine (I'm just professionally paranoid) startAjax calls (well callsBack) to doSuccess, which calls startAjax

0
source share
4 answers

, . .

,

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

success: doSuccess,
error: doError
+3

: , .

+1

, startAjax . , , gc'd, . @Dogbert , , , .

: , (), () () .

, ajax , startAjax , startAjax, . , 100% .

, google chrome, :

RangeError: Maximum call stack size exceeded

function success(){
startAjax();
}

function error(){
startAjax();
}

function startAjax(){
synchronousAjax( success, error );
}

function synchronousAjax( success, error){
Math.random() < 0.5 ? success() : error();
}

startAjax();
+1

, , ...

- , .

However, in the end, you may run into stack overflows, given the recursive nature of this approach — it never rolls back to spin the stack. Could you use setTimeoutto periodically start the process?

0
source

Source: https://habr.com/ru/post/1610064/


All Articles