javascript – Calling a function multiple times


Hello everyone, working with jQuery and need help.
You need to call the function, each time a different number of times.
The function performs calculations and enters into the database.
One activation of the function takes from 30 seconds.
How to call a function so that the second call doesn't start until the first one ends?
I thought to do it through the while loop, but it does not wait until the function is executed, and calls it again. I cannot make the function false, because then the browser will freeze completely.

while (CountTr != 0) {
                var ds = 0 // номер tr из которой вытаскиваю ссылку для функции
                var game_table = main_table.children('tr').eq(ds).children('td:first').children('a').attr('href');
                var time = main_table.children('tr').eq(ds).children('td').eq(1).children('span').html();
                analitics (game_table, time); // сама функция которую нужно вызвать несколько раз
                CountTr = CountTr - 1; 
                $('#schet').html(CountTr); // отображение на какой итерации цикл


If your function runs for a long time and is not asynchronous, then your code should work correctly (the process will simply "hang" while the function is executing). However, if you have asynchronous execution (for example, ajax calls), then the solution is simple – these are promises ( read here ). Because only the "promise" mechanism can clearly determine that the function has completed its work (or not) to transfer control further through the code.

Scroll to Top