Ajax requests in parallel

1

I have a system that has some things that you use long polling, this messes up everything, any button that you click (that makes an Ajax request) will take a long time because it has the long polling running.

Ajax by default creates a queue of requests, I want the requests to be executed at the same time, in parallel.

For example:

I'm moving the site, meanwhile, I keep an Ajax request that holds the server for 20sec.

If I click on anything or even go to another page, I have to wait for this 20sec request to finish.

I want you to give several requests at the same time, like facebook. Anyone know of any technique? Any references? I've heard of when , but that's not what I need.

Ajax for long polling

getNotifications();
    function getNotifications(lastRequest){
        if(!lastRequest){
            lastRequest = Math.round(+new Date()/1000);
        }
        $.ajax({
            url: "/ajax/get_notifications.php?last_request=" + lastRequest,
            type: "GET",
            async: true,
            success: function(data){
                data = $.parseJSON(data);
                if(data['exist_news'] == true){
                    document.getElementById('notifications').insertAdjacentHTML('afterbegin', data['news']);
                    var howMuchElement = document.getElementById('howMuchNotifications');
                    var howMuch = parseInt(howMuchElement.innerHTML);
                    var curHowMuch = data['howMuch'] + howMuch;
                    howMuchElement.innerHTML = curHowMuch;
                }
                getNotifications(data['last_request']);
            },
            error: function(error){
                showMsg(error['error']);
            }
        });
    }

NOTE: You do not have a problem with long polling.

An example request I have

function loadModal(modalPage, modalBox, button){
    $.ajax({
        url: "/modals/" + modalPage,
        async: true,
        success: function(data){
            $("#" + modalBox + " .modal-body").html(data);  
            button.removeAttribute("onclick");  
        },
        beforeSend: function(){
            $("#" + modalBox + " .modal-body").html("<img src='/images/loading.gif'> Carregando...");
        },
        error: function(){
            $("#" + modalBox + " .modal-body").html("Ocorreu uma erro!");
        }
    });
}
    
asked by anonymous 06.09.2016 / 03:12

1 answer

0

When you make an ajax request, do it asynchronously. Here is the code in Jquery, documentation example:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  console.log('oi');
});

Async is enabled by default, if you want to disable async, which would make the browser wait for the server to respond to any task, just add async: false, .

More information on the official documentation: link

    
06.09.2016 / 03:33