D
D
Dmitry2016-04-09 12:59:07
JavaScript
Dmitry, 2016-04-09 12:59:07

How to execute an AJAX request with a delay?

I work with API Vkontakte. It is necessary to send vk requests and receive a response. I do it all in a loop. However, because there is a limit on the number of requests per second, some requests receive the "Too many requests" error. The process is periodic, since it all happens in a cycle. How to implement sending a request every 0.3 seconds.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2016-04-09
@Papa_kfc

We pack the script with the ajax request into a function. After each successful completion of the request, we run setTimeout ( not setInterval ! ) and call our function again after N seconds.

function myAjax() {
    $.ajax({
        /* прочие опции*/
        ,
        success: function() {
            setTimeout(myAjax, 5000);
        }
    });
}

D
Dima Pautov, 2016-04-09
@bootd

setInterval - No?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question