jquery,如何在另一个函数结束后立即运行一个函数 [英] jquery, how to run a function as soon as another function ends

查看:88
本文介绍了jquery,如何在另一个函数结束后立即运行一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注:重写问题:

我正在尝试编写各个函数调用的以下 outside

>

例子:

 
函数f1(){
做某事1

函数f2(){
做某事2
}

运行函数1
完成后
运行函数2

(请参阅下面的jsfiddles的注释)

问题是,箱子在同一时间加载。行为应该是,加载红色,完成后,加载绿色。



如果这不是jQuery中常见的方法,请告诉我,也许我只是追逐ghost ...

解决方案

您可以简单地在第一个函数结尾处调用第二个函数。 b

  $(document).ready(function(){

function firstFunction(){
alert(this is第一个函数!);
secondFunction();
}
函数secondFunction(){
alert(this is the second function!);
}

firstFunction();
});

小提琴



或者,如果您不希望每次拨打第一个电话时调用第二个函数,您可以简单地按顺序调用它们

p>

  firstFunction(); 
secondFunction();

这是因为它会等到 firstFunction 在它移动到 secondFunction
小提琴


*Note: Re-writing question:

I am trying to write the following outside of the individual function calls:

example:

function f1(){
    do something 1
}
function f2(){
    do something 2   
}

run function 1
when done
run function 2

(See comments below for jsfiddles)

Problem is, the boxes load at the same time. Behavior SHOULD be, load red, when done, load green.

If this isn't a common method in jQuery, let me know, maybe I'm just chasing a ghost...

解决方案

You could simply call the second function at the end of the first.

 $(document).ready(function() {

  function firstFunction(){
    alert("this is the first function!");
    secondFunction();
  }
  function secondFunction(){
    alert("this is the second function!");
  }

  firstFunction();
});

fiddle

Alternatively, if you do not wish to call the second function every time you call the first, you could simply call them sequentially

firstFunction();
secondFunction();

This works because it will wait until firstFunction is done before it moves on to secondFunction fiddle

这篇关于jquery,如何在另一个函数结束后立即运行一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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