发射第三前等待的两个 - 异步函数结束时(jQuery的递延?) [英] Wait for the end of TWO asynchrounous functions before firing a third (jQuery Deferred ?)

查看:162
本文介绍了发射第三前等待的两个 - 异步函数结束时(jQuery的递延?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解jQuery的递延,但样品的inspite,我无法实现我想要做的。

所以,我想打电话asynchrounously 2的功能,而当两者都结束后,调用另一个之一。

下面是我的code

 功能1();
函数2();function3();

功能1()函数2()使一个Ajax调用我的ASP.NET应用程序,他们完美工作。 ()开始,他们必须在 function3结束。

我阅读和重读文档但function3()多恩斯等不及的其他两个的末端。能否请您提供一些我的样品,以我的理解递延?我真的需要他们来完成我的项目的重要组成部分。

非常感谢你。




奖金问题

我读过有关。当推迟......这是可能在某些&LT写; DIV> 元素是这样的功能1()运行的的,然后的函数2()运行的的等...?

更新

关于更多细节功能1()函数2():他们是非常相似的,这里有他们的身体:

 函数功能1(){
    返回$阿贾克斯({
        网址:@ Url.Action(的MyMethod,myController的),
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON
    })。完成(功能(数据){
        $。每个(数据,功能(指数){
            pushDatas(数据[指数]);
        })
    })失败(函数(结果){
        警报(function1failed);
    });
}功能pushDatas(数据)
{
    如果(!($。inArray(数据,loadedDatas)!== -1)){
        loadedDatas.push(数据);
    }
}

更新2

该函数的调用是这样的:

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
        //一些人 - 异步功能        $。当(功能1()函数2())做(function3(测试));
    }    ...< / SCRIPT>


解决方案

功能1和功能2必须返回承诺反对这个工作。例如,

 函数功能1(){
    返回$就({...});
}

现在你可以使用 $。当

  $时(功能1()函数2())来完成(fun​​ction3)。

编辑:或:

  $。当(功能1()函数2())。完成(功能(){
    function3(测试);
});

I'm trying to understand jQuery's Deferred, but inspite of samples, I can't achieve what I want to do.

So, I'd like to call 2 functions asynchrounously, and, when BOTH are ended, call another one.

Here's my code

function1();
function2();

function3();

function1() and function2() make an ajax call to my ASP.NET application, and they perfectly work. They must be ended before function3() starts.

I'm reading and reading again the documentation but function3() doens't wait for the end of the two others. Could you please provide my some sample in order to I understand Deferred ? I really need them to finish an important part of my project.

Thank you very much.


BONUS QUESTION

I've read about .when deferred... It is possible to write in some <div> element something like function1() running, then function2() running etc... ?

UPDATE

More details about function1() and function2() : They are very similar, here are they body :

function function1() {
    return $.ajax({
        url         :   "@Url.Action("MyMethod", "MyController")",
        contentType :   "application/json; charset=utf-8",
        dataType    :   "json"
    }).done(function(data) { 
        $.each(data, function(index) {
            pushDatas(data[index]);
        })
    }).fail(function (result) {
        alert("function1failed");
    });
}

function pushDatas(data)
{
    if(!($.inArray(data, loadedDatas) !== -1)) {
        loadedDatas.push(data);
    }
}

UPDATE 2

The functions are called like this :

<script type="text/javascript">
    $(document).ready(function () {
        //some others asynchrounous functions

        $.when(function1(), function2()).done(function3("test"));
    }

    ...

</script>

解决方案

function1 and function2 must return promise objects for this to work. For example,

function function1 () {
    return $.ajax({...});
}

now you can use $.when

$.when(function1(),function2()).done(function3);

Edit: or:

$.when(function1(),function2()).done(function(){
    function3("test");
});

这篇关于发射第三前等待的两个 - 异步函数结束时(jQuery的递延?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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