不推荐使用 jQuery.ajax() 方法的异步选项,现在怎么办? [英] jQuery.ajax() method's async option deprecated, what now?

查看:33
本文介绍了不推荐使用 jQuery.ajax() 方法的异步选项,现在怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 jQuery 1.8 开始,在 jQuery 中使用 async:false.ajax()弃用.
但是,当后台有正在进行的 AJAX 通信时,您看到了多少带有加载屏幕"的网页?我可能见过成千上万的.

As of jQuery 1.8, the use of async:false in jQuery.ajax() is deprecated.
But how many webpages have you seen with a "loading screen" while there is an ongoing AJAX communication in the background? I have probably seen thousands of them.

我的情况是我正在编写一个需要加载语言文件的移动应用程序.一开始我加载语言文件并从语言文件中检索按钮和其他 GUI 元素的文本.

My case is that I am writing a mobile app that needs to load a language file. And at the beginning I load the language file and I retrieve the text of the buttons and other GUI elements from the language file.

这对我来说真的很糟糕.因为如果缺少语言文件,则不应出现 GUI.那么我该如何解决呢?把我所有的代码放在 success 回调中?对我来说,这似乎不是一个好的编码习惯.我可以用其他方式解决吗?

This is really bad for me. Because if the language file is missing, the GUI shouldn't appear. So how do I solve it? Put all my code in the success callback? That doesn´t seem like a good coding practice to me. Can I solve it another way?

推荐答案

解决方案是手动添加一个overlay来防止用户与界面交互,然后在AJAX查询完成后将其移除.

The solution is to manually add an overlay to prevent the user to interact with the interface, and then remove it once the AJAX query is done.

$(function() {
    show_overlay();        

    $.ajax({
        // Query to server
    }).done(function() {
        // Verify good data
        // Do stuff
        remove_overlay();
    });
});

这篇关于不推荐使用 jQuery.ajax() 方法的异步选项,现在怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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