先后调用多个函数 [英] call multiple functions successively

查看:139
本文介绍了先后调用多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经有好几次了,但是我似乎无法得到解决方案来处理我的代码:等到带动画的函数完成后再运行另一个函数 jquery等待多个完整事件

I know this question has been asked several times, however I can't seem to get the solutions to work with my code: Wait till a Function with animations is finished until running another Function, jquery wait for multiple complete events, Wait for the end of TWO asynchrounous functions before firing a third (jQuery Deferred ?)

基本上,我想先后调用四个函数:

Basically, I would like to call four functions successively:

#1
function getJason() {
 $.getJSON(rgUrl1,function(json1){
   rgJson1 = json1; ...etc.
 });      


#2
function combineJason() {
  //code to combine 
});


#3
function divideArray() {
  //code to divide stuff 
});


#4
function doOtherStuff() {
  //code to do some other stuff
});

我需要#1才能在#2之前完成,#2要在#3之前完成, 3在#4之前完成。过去的帖子让我相信我应该使用延期和承诺。我已阅读文档 http://api.jquery.com/category/deferred-对象/ ,并试验了没有成功的例子:

I need #1 to finish before calling #2, #2 to finish before #3, and #3 to finish before #4. Past posts have led me to believe that I should use deferrds and promises. I've read through the documentation http://api.jquery.com/category/deferred-object/ and have experimented with examples without success:

function testing() {

 var deferredObj = $.Deferred();
 var test = getJson()
 var function1 = function () {

  test.done(function(){

    deferredObj.resolve();

    return deferredObj;
   });

  }

  deferredObj.done(function(){
   combineJson();
 });
}; 

此外,大多数示例处理两个函数,而我需要调用几个函数。我也注意到一些响应结合了setTimeout。对于使用计时器或暂停或延迟使用任何东西,我可能会不必要地持怀疑态度。那我不得不猜测这个函数需要多长时间。如果一个用户的计算机/连接速度较慢或什么?

Furthermore, most of the examples deal with two functions, whereas I need to call several. I've also noticed that some responses incorporate "setTimeout". I'm a bit leery, perhaps unnecessarily, about using anything with a timer or pause or delay. Wouldn't I then have to guess how long the function will take. What about if one user's computer/connection is slower or something?

推荐答案

jQuery的ajax函数已经返回一个promise。因此,如果您使用的是jQuery的ajax函数,则不需要创建新的延迟对象。

jQuery's ajax functions already return a promise. So you do not need to create new deferred objects if you are using jquery's ajax functions.

要调用函数,请继续调用函数。然后是方法,无论您需要调用多个函数。如果您的一个或多个函数需要执行一些异步任务,那么只需返回一个承诺,延迟对象将等待调用下一个,然后回调,直到返回的承诺解决。每个然后回调将传递前面然后中返回的数据(或者在返回的情况下允许解析的数据)

To get your functions called in succession: call the .then method for however many functions you need to call. If one or more of your functions need to do some async task then just return a promise, the deferred object will wait to call the next then callback till the returned promise resolves. Each then callback will get passed the data returned in the previous then (or in the case of returned promise the resolved data)

$.getJSON(rgUrl1,function(json1){
   rgJson1 = json1; ...etc.
}).then(combineJason)
.then(divideArray)
.then(doOtherStuff);      

//#2
function combineJason(someData) {
  //code to combine 
  return $.getJSON("url to some other json endpoint");
}

//#3
function divideArray(someData) {
  //code to divide stuff 
  //this function is sync so just return some data
  return someNewData;
}

//#4
function doOtherStuff(someData) {
  //code to do some other stuff
  //this function is async so return a promise;
  return $.getJSON("url to some other json endpoint");
}

JSFiddle展示混合异步/同步继承

这篇关于先后调用多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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