There is for example a similar:

count = 3; setInterval(function(){ alert(count); }, 100); 

Here's how I perform this function exactly three times?

    2 answers 2

    Each time we decrease the count , if it is zero, then we clear setInterval , using the function clearInterval .

     count = 3; var id = setInterval(function() { $.post("/ds.php", { i: count}, function(data) {}); //ваша функция alert(count--); //уменьшаем счетчик if (count <= 0) clearInterval(id); //если равен нуля, очищаем }, 100); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

    • Your code is working properly, but if you add a form submission to it like this: count = 3; var id = setInterval (function () {$ .post ("/ds.php", {i: count,}, onAjaxSuccess); function onAjaxSuccess (data) {} alert (count--); if (count <= 0 ) clearInterval (id);}, 100); it never stops working - Shevtsov Eugene
    • @ShevtsovEugene corrected under your code - Mr_Epic
    • @Other what's the problem? Requests are sent with the necessary count , asynchronously, I see no problems doing so. - Mr_Epic
    • one
      thank. =) But by the way, no, not everywhere was one. - Shevtsov Eugene
    • @ShevtsovEugene Need help with this or can you handle it? - Mr_Epic

     let count = 3; function tmp(){ alert(count); if(count > 1){ setTimeout(tmp, 100); // Не используем интервал, ибо равные промежтки получить сложно (см. https://habrahabr.ru/post/138062/) count--; } } tmp(); 

    • The question was exactly in the form in which there is, how to perform it 3 times, and you redid everything. Just provide the code is not enough, add an explanation of it. - Mr_Epic
    • @Mr_Epic, it seems not much has changed, but it works and not bad, the intervals have a bad catch with the execution time, I am not friends with them and I do not advise others :) This is IMHO. - user207618