Javascript simple loop

I saw the following code:

while(ajaxCallInProgress)
        {
        }
        ajaxCallInProgress = true;

What is the use of this empty loop in javascript? What is he doing? Thanks in advance...

+3
source share
7 answers

if it ajaxCallInProgressis a true expression, it will be an infinite loop (and therefore, the interpreter will freeze forever).

In most cases, it makes sense to do something, while a specific condition is true, but in all these cases, the condition to be checked should at some point establish a false value inside the loop body.

Since ECMA- / Javascript does not support multiple threads (I just ignore web workers here), this variable cannot be changed anywhere else.

Conclusion

, , , .

+5

, ajaxCallinProgress false. , CPU, .

, , - .

, .

+2

, , AJAX. , ajaxCallInProgress , , false -, true, . , , , .

+1

-. , , ajaxCallInProgress. , Ajax , , , , ajaxCallInProgress, .

+1

ajaxCallInProgress , . Javascript , . , , ajax .

, :

$. get ('url', {params: true}, () {  /* ajax */});

0

This code seems to be waiting for an ajax call to be answered. Basically, this loop waits until a global variable ajaxCallInProgressis set to falseanother JavaScript function.

So, when the answer is received, the corresponding JavaScript function will install ajaxCallInProgress = false;, and your loop will stop.

0
source

If "ajaxCallInProgress" is true at the beginning and the script does not run async, it would hang your browser.

If he starts async, it will consume a lot of CPU, but the browser will not hang .. it's hard to say anything else from this code.

0
source

Source: https://habr.com/ru/post/1791469/


All Articles