订购异步JavaScript事件 [英] Ordering of asynchronous javascript events

查看:76
本文介绍了订购异步JavaScript事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

$("#submit_financials").live('click', function(event){
    event.preventDefault();

    // using serialize here to pass the POST variables to the django view function
    var serialized_data = $("#financials_filter_form").serialize()

    $.post("/ajax/custom_filter/", serialized_data, function(response){
        // create a graph
    });
    $.post("/ajax/force_download/", serialized_data, function(response){
        alert('hello');
    });

});

然而,当我这样做code,我得到的回应你好的之前图。这究竟是为什么?而我将如何改变这使得我第一次拿到图?

However, when I do this code, I get the response 'hello' before the graph. Why is this happening? And how would I change this such that I get the graph first?

推荐答案

异步,你永远无法知道哪个函数运行\完成第一...

Async, you can never know which function runs\ finish first...

在思考像讲一群人跑1英里你知道谁将会先完成异步操作? (是的,乔恩斯基特,那么查克·诺里斯...)

Think on async operations like telling a group of people to run 1 mile, do you know who will finish first? (Yes, Jon skeet, then Chuck Norris...)

您可以使用一个callack运行第二个AJAX:

You can use the a callack to run the second ajax:

$.post("/ajax/custom_filter/", serialized_data, function(response) {
    // create a graph
    ...
    ...

    $.post("/ajax/force_download/", serialized_data, function(response) {
        alert('hello');
    });
});​

这篇关于订购异步JavaScript事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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