Is it safe to use ajax requests many times and repeatedly?

6

I want to create a 'mini server' for me to use on my site (tumblr), for real communication with my visitors and one way to do this is to use ajax requests. When the site loads, it requests a JSON file, when I receive this file the request is made again and so on. When I update the JSON file, the site gets it almost immediately, so the data will be updated for the visitors.

I have doubts about doing this, I do not know if it can crash the site, get a lot of internet or have some bad effect. Do you think I can do this or is there any better way?

    
asked by anonymous 08.03.2014 / 02:24

1 answer

4

Everything will depend on the standard size of your response and query performance. The ideal is to traffic only the data that will be consumed. You can, for example, pass on each request the ID of the last conversation that the user sought, and bring the following from that one.

One way to do this is to use a javascript function that every x seconds retrieves the data. However, it is important that new requests are made only after the last ones are terminated - so it is not advisable to use setInterval , since if the request takes more seconds to process than the query firing interval, you with a queuing of requisitions. You guarantee this through recursion into a non-anonymous self-executable function:

(function loop() {
    setTimeout(function() {
        // Inteligência da requisição
        if (complete)
            loop(); // Executa novamente a função quando a requisição atual terminar. Pode ser o **complete** do jQuery.ajax, por exemplo
    }, 5000); // Função, intervalo em milissegundos
})();
    
08.03.2014 / 03:02