Multiple ajax calls at same time

后端 未结 2 2078
失恋的感觉
失恋的感觉 2020-11-30 00:54

I have developed some websites and I always stumble a the same point: multiple ajax calls. I have a main page where all the content is loaded asynchronously. When the page i

相关标签:
2条回答
  • 2020-11-30 01:13

    CLOSURES

    Closures are a little mind-blowing at first. They are a feature of javaScript and several other modern computing languages.

    A closure is formed by an executed instance of a function that has an inner function (typically an anonymous event handler or named method) that needs access to one or more outer variables (ie. variables that are within the outer function but outside the inner function). The mind-blowing thing is that the inner function retains access to the outer variables even though the outer function has completed and returned at the time that the inner function executes!

    Moreover, variables trapped by a closure are accessible only to inner functions and not to the further-out environment that brought the closure into being. This feature allows us, for example, to create class-like structures with private as well as public members even in the absence of language keywords "Public" and "Private".

    Closures are made possible by inner functions' use of outer variables suppressing javaScript's "garbage collection" which would otherwise destroy the outer function's environment at some indeterminate point after completion.

    The importance of closures to good, tidy javaScript programming cannot be overstressed.

    In the code below the function getData() forms, at each call, a closure trapping id1 and id2 (and url), which remain available to the anonymous ajax response handler ($.get's third argument).

    $(document).ready(function() {
    
        function getData(url, id1, id2) {
            $.get(url, Common.genSafeId(), function(data) {
                $(id1).html(data);
                $(id2).addClass("empty");
            });
        }
    
        getData('/activity', '#stream', '#load_activity');
        getData('/messages', '#message', '#load_messages');
        getData('/deals', '#new_deals_container', '#load_deal');
        getData('/tasks', '#task_frames', '#load_task');
    
    });
    

    Thus, rather than writing four separate handlers, we exploit the language's ability to form closures and call the same function, getData(), four times. At each call, getData() forms a new closure which allows $.get's response handler (which is called asynchronously when the server responds) to address its DOM elements.

    0 讨论(0)
  • 2020-11-30 01:25

    Make sure you have different callbacks for each ajax call, it sounds like you are trying to use the same function for all four, thus when they are called out of order (because they take different amounts of time server-side), they are rendering in the wrong place. If you insist on using the same function for all your callbacks, then you have to put something in the payload so that the callback knows where to render to.

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