使用同步ajax调用有什么缺点?

tra*_*ss0 6 ajax jquery class prototypejs

这个问题肯定可以应用于jQuery,但在这种情况下,我指的是Prototype.在原型文档中,它说,

由于同步使用相当令人不安,并且通常味道不好,因此您应该避免更改此设置.认真.

我不确定使用同步ajax调用有什么缺点.似乎有许多实例必须等待调用返回(不使用特定的回调函数).例如,我目前使用Prototype onSuccess, onFailure and onComplete来处理其余的代码.

但是,我使用的Web服务(所有内部)跨越大多数项目,我的任务是创建更多可重用的代码.一个示例是返回客户属性的客户类.一个简单的例子(请记住,我只显示基本功能以保持简单):

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();

    } 

});
Run Code Online (Sandbox Code Playgroud)

因此,使用这个简单的类我可以在任何项目中执行以下操作来获取客户信息.

var customer = new Customer(1);

//now I have the customer info
document.write(customer.customerInfo.firstName);
Run Code Online (Sandbox Code Playgroud)

使用上面的代码不会打印出客户的名字.这是因为ajax调用是异步的.它将执行document.writeWeb服务是否带回客户数据.但是,在数据恢复并设置了客户变量之前,我不想做任何事情.为了解决这个问题,我将ajax调用设置为synchronous,这样浏览器就不会继续运行直到new Customer(1);完成.

这种方法似乎有效(将异步设置为false)但是阅读Prototype文档会让我停下来.使用这种方法有什么缺点?有没有其他方法可以做到,效率更高等等?

我将不胜感激任何反馈.

Ray*_*nos 10

让我们提醒您,JavaScript是单线程的

同步IO调用BLOCKS THETIRERE THREAD

一个简单的解决方法是使用回调来使用异步样式编程.

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);
});
Run Code Online (Sandbox Code Playgroud)


cwa*_*ole 6

那么,除了事实,这是非常不JavaScript的,它有这样愉快的效果完全阻止浏览器的UI.这不是一个小问题.

做了一些研究,找到了很酷的东西.原型基于XMLHttpRequest.open的"异步".根据维基百科,这不是规范的必要部分,它将阻止"onreadystatechange".


Xed*_*mal 2

大多数人不赞成同步 ajax 调用,因为它会冻结 UI,直到它完成,因为它不允许代码继续执行,直到它完成。我想你可能会说,这会导致界面出现口吃。