Home > Web Front-end > Front-end Q&A > Is async an attribute of ajax?

Is async an attribute of ajax?

青灯夜游
Release: 2022-01-19 18:19:51
Original
3112 people have browsed it

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();
});
Copy after login

① 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template