是asnyc:真的假的那么糟吗? [英] Is asnyc : false really that bad?

查看:660
本文介绍了是asnyc:真的假的那么糟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题由两部分组成。

My question consists of two parts.

在我的网页上,我创造了我的一些动态的div使用AJAX后调用获得从数据库的一些信息。然后创建我的div。我的结构如下。

In my webpage, i am creating some of my divs dynamically using ajax post calls to get some information from db. Then i create my divs. My structure is as follows.

<html>
  <body>
    <script type="text/javascript" src="some.js"></script>
    <script type="text/javascript" src="createDivs.js"></script>
    <script type="text/javascript" src="useThatDivs.js"></script>
    <script type="text/javascript" src="useThatDivsMore.js"></script>
  </body>
</html>

下面,createDivs.js确实Ajax调用与异步:假的(因为后面的脚本,我用这两个div)(和顺便说一句,这是一个后调用服务器),并获得从数据库的信息,然后创建根据信息的div。

Here, createDivs.js does the ajax call with async:false (because on later scripts, i use these divs)(and btw, it is a post call to the server), and gets the info from db, then create the divs according to the info.

所以,我的问题是。

  • 1)是否Ajax调用应该永远是异步?然后,我使用AJAX的是 完全没用/错在这里?
  • 2)如果异步:假的就是DRE precated,我应该使用这个东西,而不是 方法?
  • 1) Does ajax calls should always be async? Then, my usage of ajax is completely useless/wrong here?
  • 2) If async:false is dreprecated, what should i use instead of this approach?

推荐答案

AJAX的使用回调成功错误 。如果您正在使用同步AJAX调用,比你锁定你的网站的所有接口,直到所有的响应就完成了。

Use callbacks of AJAX success and error. If you are using synchronous AJAX calls, than you are locking all interfaces of your website, till all responses are done.

$.ajax({
    url: '#',
    data: {id: 1},
    success: function(dataFromServer) {
       callFunctionToProcess(dataFromServer);
       /* OR */
       var directlyProcess = dataFromServer;
    },
    error function(data) {
       var yourRequestHasFailed = true; // e.g. you get 500 server error
    }
});

编辑jfriend00

它不会锁定你的浏览器中的任何其他选项,因为它不会锁定任何以下的:操作系统,BIOS,别墅门,车门或其他任何可锁定的项目

It will not lock any other tabs of your browser, as it will not lock any of the following: OS, BIOS, house doors, car doors or any other lockable item.

这篇关于是asnyc:真的假的那么糟吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆