JavaScript中的函数执行顺序

JavaScript中的函数执行顺序,javascript,ajax,Javascript,Ajax,我有3个功能: function selected_feature_intervencoes(event){ store_intervencoes.removeAll(); for(var i=0; i<ruas_intervencoes.selectedFeatures.length; i++){ trecho_selecionado = parseInt(ruas_intervencoes.selectedFeatures[i].attributes

我有3个功能:

function selected_feature_intervencoes(event){

    store_intervencoes.removeAll();
    for(var i=0; i<ruas_intervencoes.selectedFeatures.length; i++){

        trecho_selecionado = parseInt(ruas_intervencoes.selectedFeatures[i].attributes.k_n_rua);
        toponimo_selecionado = ruas_intervencoes.selectedFeatures[i].attributes.toponimia;
        valor_trecho += parseFloat(ruas_intervencoes.selectedFeatures[i].attributes.orcamento_total);
        ajaxFunction();
        }                   
    };

function ajaxFunction(){
    Ext.Ajax.request({
        url: 'php/grid.php',
        method: 'GET',
        success: sucesso(i),
        failure: fracasso,
        params: {'k_n_rua': trecho_selecionado[i]}
        });
    }


function sucesso (result, request) {

    jsonData = Ext.util.JSON.decode(result.responseText);            
    if (!jsonData.rows) {
        jsonData = Ext.util.JSON.decode('{"rows" :[{"fk_n_rua":"' + trecho_selecionado[] +  '","nome_intervencao":"","prioridade":"","orcamento":"","toponimia":"' + toponimo_selecionado + '"}]}');
}
 store_intervencoes.loadData(jsonData, true);       
 document.getElementById('total_intervencoes').value = valor_trecho.toFixed(2) + ' €';

}
但我想按以下顺序执行它们:

ajaxFunction();
ajaxFunction();
sucesso ();
sucesso ();
ajaxFunction();
sucesso ();
ajaxFunction();
sucesso ();

谁能告诉我我是怎么做到的吗?

你可以使用像这样的promise库。Q允许将异步调用链接在一起,语法如下所示:

Q.fcall(ajaxFunction)
    .then(sucesso)
    .then(ajaxFunction)
    .then(sucesso)
    .done();

直接调用服务器,而不是使用AJAX。这样,它将等到收到响应后再进入下一步。

使ajax函数同步将此添加到ajax调用中:async:false一个问题是您没有正确调用
success
回调函数——它在调用ajax函数时立即执行,而不是在调用完成时执行。我认为你应该选择成功:成功(没有括号),不要这样做。同步AJAX是愚蠢的。AJAX应该是异步的。使它们同步会破坏整个目的。