Home  >  Article  >  Web Front-end  >  Is async an attribute of ajax?

Is async an attribute of ajax?

青灯夜游
青灯夜游Original
2022-01-19 18:19:513079browse

async is an attribute of ajax. The async attribute is used to specify whether the ajax request is processed asynchronously. The default value is true (asynchronous processing); after ajax is executed, the subsequent scripts will continue to be executed until the server returns data, triggering the success callback function success in ajax. At this time Two threads are executed.

Is async an attribute of ajax?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

async is an attribute of ajax.

The async attribute is used to specify whether the ajax request is processed asynchronously. The value is a Boolean type and the default value is true (asynchronous processing).

$.ajax async: The role of true or false

When writing an ajax request, the async attribute is not written by default. Async defaults to true, that is, asynchronous mode. After ajax is executed, the subsequent scripts will continue to be executed until the server returns data, triggering the success callback function success in ajax. At this time, two threads are executed.

If async is set to false, the request is a synchronous request. The script behind ajax will not be executed until the server returns data. Only when the ajax request is completed, the script behind ajax will continue to be executed.

Example:

var App = function () {
    this.Startup = function () {
        this.Test();
    };

    this.Test = function () {
        var name = null;

        $.ajax({
            type: 'POST',
            url: '/Home/GetName',  // 本地测试接口
            async: true, 
            success: function (result) {
                name = result.name;
            }
        });

        alert(name);
    };
};

$(document).ready(function () {
    var app = new App();
    app.Startup();
});

① When async:true, the asynchronous request will continue to execute the script behind ajax, so alert

will be executed Is async an attribute of ajax?

② When async:false, the synchronous request will wait for ajax execution to be completed before executing the script behind it, so name will be in the ajax success callback function success is assigned a value.

Is async an attribute of ajax?

[Related tutorial recommendations: AJAX video tutorial]

The above is the detailed content of Is async an attribute of ajax?. 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