Home  >  Article  >  Web Front-end  >  A simple understanding of the combination of synchronization, closure and asynchronous requests in js

A simple understanding of the combination of synchronization, closure and asynchronous requests in js

php是最好的语言
php是最好的语言Original
2018-08-01 11:07:052223browse

Let’s look at the synchronization request first

The background received is 0~10
The ajax callback output is also 0~10

for (var index = 0; index <= 10; index++) {
    $.ajax({
        async: false,//同步
        url: &#39;/HelloWorld&#39;,
        type: &#39;POST&#39;,
        dataType: &#39;html&#39;,
        data: {index: index}
    }).done(function () {
        console.log(index);
    })
}

A simple understanding of the combination of synchronization, closure and asynchronous requests in js

A simple understanding of the combination of synchronization, closure and asynchronous requests in js

After switching to asynchronous
The data received in the background has changed and is not expected 0~10

A simple understanding of the combination of synchronization, closure and asynchronous requests in js

ajax The same is true for the callback output, which becomes 11 11
A simple understanding of the combination of synchronization, closure and asynchronous requests in js

The order of ajax execution is after the for loop execution, i becomes 11
Need to maintain alignment when ajax is executed The reference of i can achieve the expected effect

for (var index = 0; index <= 10; index++) {
    (function (num) {//形参
        $.ajax({
            async: true,//异步
            url: &#39;/HelloWorld&#39;,
            type: &#39;POST&#39;,
            dataType: &#39;html&#39;
        })
            .done(function () {
                console.log(num);
            })
    })(index)//实参}

In this way, the value received by the background can be consistent with the value output by the foreground

Look at the synchronization request first
The background received is 0 ~10
The ajax callback output is also 0~10

for (var index = 0; index <= 10; index++) {
    $.ajax({
        async: false,//同步
        url: &#39;/HelloWorld&#39;,
        type: &#39;POST&#39;,
        dataType: &#39;html&#39;,
        data: {index: index}
    }).done(function () {
        console.log(index);
    })
}

A simple understanding of the combination of synchronization, closure and asynchronous requests in js

A simple understanding of the combination of synchronization, closure and asynchronous requests in js

After changing to asynchronous,
received by the background The data has changed, which is not the expected 0~10
A simple understanding of the combination of synchronization, closure and asynchronous requests in js

The callback output of ajax is the same, it has become 11 11
A simple understanding of the combination of synchronization, closure and asynchronous requests in js

ajax execution After the for loop is executed sequentially, i becomes 11
It is necessary to maintain a reference to i during ajax execution to achieve the expected effect

for (var index = 0; index <= 10; index++) {
    (function (num) {//形参
        $.ajax({
            async: true,//异步
            url: &#39;/HelloWorld&#39;,
            type: &#39;POST&#39;,
            dataType: &#39;html&#39;
        })
            .done(function () {
                console.log(num);
            })
    })(index)//实参}

In this way, the value received by the background can be compared with the value output by the foreground Values ​​are consistent

Related articles:

Summary of methods and differences between synchronous and asynchronous processing in js_javascript skills

ajax Synchronous request and Difference analysis of asynchronous requests

Related videos:

Qianfeng Education PHP asynchronous communication framework Swoole interpretation video tutorial

The above is the detailed content of A simple understanding of the combination of synchronization, closure and asynchronous requests in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn