使用jquery同步调用

发布于 2024-09-03 18:15:24 字数 37 浏览 1 评论 0原文

我可以使用 jQuery AJAX API 进行同步调用吗?

Can I make use of jQuery AJAX API and make a synchronous calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-09-10 18:15:25

就像奥巴马会说的:是的,你可以!

jQuery .ajax()

在 .ajax() 处理程序中进行设置

async = false

即可解决问题。

Like Obama would say: YES YOU CAN !

jQuery .ajax()

Setting

async = false

within the .ajax() handler will do the trick.

倾`听者〃 2024-09-10 18:15:25

虽然 jQuery 可以通过设置synch:false 属性来进行同步 AJAX 调用,但这会导致浏览器挂起,直到 AJAX 完成。使用像 Frame.js 这样的流控制库,您可以在不占用浏览器的情况下进行同步调用:

$.each(ajaxObjects, function(i, ajaxCall){
    Frame(function(next)){ // declare the callback next here

        ajaxCall.complete = function(data){
            // do something with the data
            next(); // go to the next ajax call
        }
        $.ajax(ajaxCall);

    });
}
Frame.init();

这一系列的 AJAX 调用将按顺序进行,每个调用都会等待前一个调用完成,而不会导致浏览器挂起。还有一个额外的好处,即数据以可预测的顺序从 ajax 调用返回,而不是以随机顺序返回的异步调用。

While jQuery can make sync AJAX calls by setting the synch:false property, this causes the browser to hang until the AJAX completes. Using a flow control library like Frame.js enables you to make synchronous calls without tying up the browser:

$.each(ajaxObjects, function(i, ajaxCall){
    Frame(function(next)){ // declare the callback next here

        ajaxCall.complete = function(data){
            // do something with the data
            next(); // go to the next ajax call
        }
        $.ajax(ajaxCall);

    });
}
Frame.init();

This series of AJAX calls will be made in order, each waiting for the previous to complete, without making the browser hang. Also has the added benefit that the data returns from the ajax calls in a predictable order, as opposed to asynchronous calls which return in a random order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文