如何使Javascript同步运行

如何使Javascript同步运行,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,在我的asp.NETMVC应用程序中,我点击了一个指向javascript的按钮,该javascript调用 function OnButtonClick(s, e, startUrl, progressUrl) { Fetch(progressUrl); ImportUpdate(startUrl); } Fetch和ImportUpdate是控制器操作的ajax jquery function Fetch(progressUrl) { positio

在我的asp.NETMVC应用程序中,我点击了一个指向javascript的按钮,该javascript调用

function OnButtonClick(s, e, startUrl, progressUrl) {
    Fetch(progressUrl);
    ImportUpdate(startUrl);
   }
Fetch和ImportUpdate是控制器操作的ajax jquery

 function Fetch(progressUrl) {
        positionDate = ReportingPositionDate.GetDate().toDateString();
        $.ajax({
            type: 'POST',
            url: "@Url.Action("BloombergFet", "ImportData")",
            data: JSON.stringify({ positionDate: positionDate }),
            dataType: "text",
            contentType: "application/json; charset=utf-8",
            beforeSend: function () { lpBloomberg.Show(); },
            success: function (msg) {
                ImportSuccessMessage.SetText(msg);
                lpBloomberg.Hide();
                lpImport.Show();

            },
            error: function (xhr, textStatus, errorThrown) {
                lpBloomberg.Hide()

            }
        });
    }

function ImportUpdate(progressUrl) {
        positionDate = ReportingPositionDate.GetDate().toDateString();
        myProgressBar.Show;
        $.ajax({
            type: 'POST',
            url: "@Url.Action("ProcessImportRecord", "ImportData")",
            data: JSON.stringify({ positionDate: positionDate }),
            dataType: "text",
            contentType: "application/json; charset=utf-8",
            beforeSend: function () { lpImport.Show(); },
            success: function (msg) {
            ImportDataGridView.PerformCallback();
            ImportSuccessMessage.SetVisible(true);
            ImportSuccessMessage.SetText(msg);
            lpImport.Hide();
        },
        error: function (xhr, textStatus, errorThrown) {
            ImportErrorMessage.SetVisible(true);
            ImportErrorMessage.SetText(xhr.statusText)
        }
    });
    }
目前,同时调用方法
Fetch(progressUrl)
ImportUpdate(progressUrl)
。我希望
Fetch(progressUrl)
完成,然后ImportUpdate运行


我如何做到这一点。谢谢你的帮助

在第一个函数Fetch(progressUrl)的成功块中调用第二个函数ImportUpdate(progressUrl),如下所示:

然而,正如James所指出的,如果您想在每次调用Fetch之后调用ImportUpdate,则最好将它们组合起来,除非您在其他地方单独调用ImportUpdate,而Fetch不是首先调用的

顺便说一句,Kevin B.可能指的回调与jQuery.post()函数一起使用,您可以这样使用:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second finished" );
});
因此,您不需要将函数调用放入当前获取函数的成功回调中,而是将其放入。完成回调如下:

.done(function() {
    ImportUpdate(startUrl);
  })
  .fail(function() {
    //handle errors
  })

在第一个函数Fetch(progressUrl)的成功块中调用第二个函数ImportUpdate(progressUrl),如下所示:

然而,正如James所指出的,如果您想在每次调用Fetch之后调用ImportUpdate,则最好将它们组合起来,除非您在其他地方单独调用ImportUpdate,而Fetch不是首先调用的

顺便说一句,Kevin B.可能指的回调与jQuery.post()函数一起使用,您可以这样使用:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second finished" );
});
因此,您不需要将函数调用放入当前获取函数的成功回调中,而是将其放入。完成回调如下:

.done(function() {
    ImportUpdate(startUrl);
  })
  .fail(function() {
    //handle errors
  })

ImportUpdate(progressUrl)
放入成功回调函数中,用于
Fetch(progressUrl)
ImportUpdate(progressUrl)
放入成功回调函数中,用于
Fetch(progressUrl)
回调。使用回调。如果您总是希望先获取,然后再导入ImportUpdate,我建议您将它们合并到一个API调用中。谢谢James。这很有效Kevin B您是否有回调示例。使用回调。如果您总是希望先获取,然后再导入ImportUpdate,我建议您将它们合并到一个API调用中。谢谢James。你有没有举个例子