What are the drawbacks of using synchronous ajax call?

前端 未结 4 1295
醉酒成梦
醉酒成梦 2020-11-29 12:41

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

相关标签:
4条回答
  • 2020-11-29 13:00

    Well, in addition to the fact that it is very not JavaScript, it has such pleasant effects as COMPLETELY BLOCKING THE BROWSER'S UI. That is not a minor issue.

    Did some research, found cool things. Prototype bases "asynchronous" off of XMLHttpRequest.open. According to Wikipedia, this is not something which is not a required part of the specification and it will prevent "onreadystatechange".

    0 讨论(0)
  • 2020-11-29 13:02

    Well, i don't find that it is a problem to block the UI if the user is generating a loading image while the synchronous call is done. It could be the same as submitting the form.

    0 讨论(0)
  • 2020-11-29 13:14

    Let's remind you that JavaScript is single-threaded

    A synchronous IO call BLOCKS THE ENTIRE THREAD

    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);
    });
    
    0 讨论(0)
  • 2020-11-29 13:17

    Most people frown upon synchronous ajax calls because it'll freeze the UI up until it has completed, as it will not allow code to continue until it's completion. Makes for a stutter in the interface I guess you could say.

    0 讨论(0)
提交回复
热议问题