简体   繁体   English

同步Ajax调用 - 或:如何等待所有内容加载继续?

[英]Synchron Ajax Call - or: How to wait until everything is loaded to go on?

I have a single page application with ASP.Net and JavaScipt / jQuery. 我有一个ASP.Net和JavaScipt / jQuery的单页面应用程序。

In my Page initialisation there is much to load, eg 6 dropdowns, a bunch of textboxfilles, configuration files etc. 在我的页面初始化中有很多要加载,例如6个下拉列表,一堆文本框文件,配置文件等。

Now I have a gridview to load. 现在我有一个gridview加载。 The problem ist, that the gridview needs data from the textboxes and dropdowns. 问题是,gridview需要来自文本框和下拉列表的数据。

So how to know, when the asynchron ajax calls of everything else is finished? 那么如何知道,当异步ajax调用其他所有东西时就完成了?

Simply put a settimeout() for the loading grid and wait a bunch of time, eg 300ms? 只需为加载网格设置一个settimeout()并等待一段时间,例如300ms?

You can use callback function which will get called when response of ajax request arrives. 您可以使用回调函数,该函数将在ajax请求的响应到来时被调用。

$.ajax({
    type:"method_type",
    url: "your_url",
    dataType: "data_type",
    async: true,
    success: function(data){
        callback(data);
    }
});

function callback(d){
  // grid functionality
}

Update: 更新:

$.when($.ajax("req1"), $.ajax("req2"))
  .then( callback, errfun);

This will execute the function callback when both ajax requests are successful, or errfun if either one has an error. 这将在两个ajax请求成功时执行函数callback ,或者如果任何一个有错误则执行errfun

There should be a success callback. 应该有一个success回调。 In asynchronous programming, you stick event-driven code inside callback functions. 在异步编程中,您将事件驱动的代码粘贴在回调函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM