Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance multiple ajax call in Javascript

I am developing a module in Javascript, that in some strange cases, will launch multiple AJAX call to a web service in a range of Ips. For example:

var _requests = [],
    _found = false;

for( var i = 1; (i < 255 && !_found); i++ ){
    _requests.push( $.ajax({
        url: "http://192.168.1." + i + "/service?action=example",
        type: "GET",
        success: _callback
    }) );
}

var _callback = function( data, status, petitionInfo ) {

    _found = true;

    var _requestsToCancel = _requests.length;

    while( _requestsToCancel-- ){
        _requests[_requestsToCancel].abort();  
    }

};

In terms of performance, is it necessary (convenient), cancel every AJAX petition? Or it is irrelevant? When one ip responses, no other ip will do it. Putting a low timeout would be better for performance?

like image 455
javifm Avatar asked Jun 27 '26 22:06

javifm


1 Answers

As you have already started the requests, each server will receive a request.

I'm assuming however that only one will respond and the others will time out. In which case I would recomend cancelling the other requests to save resources on the client.

like image 106
Joey Ciechanowicz Avatar answered Jun 30 '26 11:06

Joey Ciechanowicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!