什么是使用同步的Ajax调用的弊端? [英] What are the drawbacks of using synchronous ajax call?

查看:205
本文介绍了什么是使用同步的Ajax调用的弊端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题肯定可以被应用到jQuery的,但在这种情况下,我指的原型。在原型文档,它说,

This question could surely be applied to jQuery but in this case I am referring to Prototype. In the Prototype doc it says,

由于同步使用是相当   令人不安的,通常口感不好,你   应避免改变这一点。认真。

Since synchronous usage is rather unsettling, and usually bad taste, you should avoid changing this. Seriously.

我不知道有什么用同步Ajax调用的弊端。似乎有很多情况下,你必须等待调用返回(不使用特定的回调函数)。比如我目前使用的原型的的onSuccess,onFailure和的onComplete 处理的code中的其余部分。

I'm not sure what the drawbacks of using a synchronous ajax call. It seems that there are many instances where you must wait for the call to return (without using the specific callback functions). For example I currently use Prototype's onSuccess, onFailure and onComplete to handle the rest of the code.

然而,Web服务我使用(所有内部),跨度最大的项目,我一直负责创造更多的可重复使用的code。一个例子是,返回客户特性的客户类。一个简单的例子(记住,我只显示的基本功能保持简单):

However, the web services I use (all in-house) span most projects and I have been tasked with creating more reusable code. An example would be a customer class that returns customer properties. A simple example (keep in mind I am only showing the basic functions to keep it simple):

Customer = Class.create({ 

    initialize: function(customerId) { 

        new Ajax.Request('some-url', {
            method: 'get',
            parameters: {
                customerId: customerId
            },
            onSuccess: this.setCustomerInfo.bind(this)
        }

    },

    setCustomerInfo: function(response) {

        //for the sake of this example I will leave out the JSON validation

        this.customerInfo = response.responseText.evalJSON();

    } 

});

因此​​,使用简单的类我可以做下列的任何项目,获得了客户的信息。

So, using that simple class I can do the following in any project to get the customer info.

var customer = new Customer(1);

//now I have the customer info
document.write(customer.customerInfo.firstName);

使用上述code不会打印出客户的名字。这是因为Ajax调用是异步的。它将执行文件撰写 Web服务是否不带回来的客户数据。但我不希望做任何事情,直到数据已经回来了与客户变量设置。为了解决这个问题我设置了Ajax调用同步等等,直到新客户(1)浏览器将不会继续。完成

Using the above code will not print out the customer's first name. This is due to the ajax call being asynchronous. It will execute the document.write whether or not the web service brought back the customer data. But I don't want to do anything until the data has come back and the customer variable is set. To fix this I set the ajax call to synchronous so the browser will not continue on until the new Customer(1); is finished.

这个方法似乎工作(异步设置为false),但读的原型文档给我停下来。这是使用这种方法的缺点?是否有另一种方式做到这一点,更有效,等等?

This method seems to work (setting asynchronous to false) but reading the Prototype docs gives me pause. What is the drawback of using this method? Is there another way to do it, more efficient, etc?

我会AP preciate任何反馈。

I would appreciate any feedback.

推荐答案

让我们提醒你,JavaScript是单线程

Let's remind you that JavaScript is single-threaded

一个同步IO调用的块中的整个主题

有一个简单的解决方法是使用回调使用异步编程风格。

A simple fix is to use asynchronous style programming using callbacks.

Customer = Class.create({     
    initialize: function(customerId, cb) { 
        new Ajax.Request('some-url', {
            method: 'get',
            parameters: {
                customerId: customerId
            },
            onSuccess: (function() {
                this.setCustomerInfo.apply(this, arguments);
                cb.apply(this, arguments);
            }).bind(this)
        }
    },
    setCustomerInfo: function(response) {
        //for the sake of this example I will leave out the JSON validation
        this.customerInfo = response.responseText.evalJSON();
    }   
});

var c = new Customer(1, function() {
     document.write(customer.customerInfo.firstName);
});

这篇关于什么是使用同步的Ajax调用的弊端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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