ExtJs: What will be the sequence of code execution when calling ajax?

Given the fact that Javascript is a single-threaded language, what will be the sequence of execution in the following code in ExtJs?

FuncA();

Ext.Ajax.request({
    url: 'ajax.php',
    params: {
        id: 1
    },
    success: function(response){

        FuncB();
    }
});

FuncC();

FuncD();

Here the sequence of functions will be ACDB?

or

Is it possible that FuncB () is called before FuncC () or FuncD ()? If so, in what conditions?

Thanks in advance for your help.

+3
source share
3 answers

The order will always be ACDB if the ajax request is not synchronized, but it is not really recommended to use it because it makes the user interface freeze.

For example, try this (in Chrome, preferably)

function f1() {
    var s = '';
    for (var i = 0; i < 10000000; ++i) {
        s += 'a';
    }
}

function f2() {
    var s = '';
    for (var i = 0; i < 10000000; ++i) {
        s += 'b';
    }
}

Ext.require('Ext.Ajax');

Ext.onReady(function() {

    var d;
    Ext.Ajax.request({
        url: 'data.json',
        success: function(){
            console.log('finished');
        }
    });

    d = new Date();
    f1();
    console.log(new Date() - d, 'f1 done');
    d = new Date();
    f2();
    console.log(new Date() - d, 'f2 done');

});

, 1 , ajax , 7 ( ). , f1/f2.

+2

{B} {B} D {B}
b , {b}. .
: -
javascript . queue , . , funcC, funcC, ii , , funcB ( ajax ), funcB

0

Please note that javascript code runs from top to bottom. Thus, in this case, the execution of your code starts with funcA()and ends with funcD(), but funcB()can be called at any time, because it completely depends on how soon the ajax.phpresponse returns.

-1
source

All Articles