如何在一个JQuery语句中运行多个函数

时间:2017-02-08 18:35:21

标签: jquery callback

我试图在Jquery中调用两个单独的函数:alertMe1AlertMe2。我无法弄清楚正确的语法。我不想内联定义函数,因为我需要在文档中稍后重用它们。我还需要第一个函数alertMe1在第二个函数alertMe2启动之前完全完成。我究竟做错了什么?

$( document ).ready(function() {
    alertMe1, alertMe2
});

function alertMe1() {
    alert("Alert1");
} 

function alertMe2() {
    alert("Alert2");
} 

2 个答案:

答案 0 :(得分:0)

您可以像往常一样调用函数:

$( document ).ready(function() {
    alertMe1();
    alertMe2();
});

答案 1 :(得分:0)

有一点需要注意的是,只要您声明它们,Javascript编译器就不关心您声明函数的顺序。然后你可以简单地使用一个回调函数,作为参数传递给你的第一个函数。

P[0] sends Q_send[0] to P[3] = -2.12
P[0] sends Q_send[1] to P[3] = -2.12
P[0] sends Q_send[2] to P[3] = 4.12
P[0] sends Q_send[3] to P[3] = 4.12
P[0] receives Q_recv[0] from P[1] = 1.00
P[0] receives Q_recv[1] from P[1] = 1.00
P[0] receives Q_recv[2] from P[1] = 1.00
P[0] receives Q_recv[3] from P[1] = 1.00

P[1] sends Q_send[0] to P[0] = -2.12
P[1] sends Q_send[1] to P[0] = -2.12
P[1] sends Q_send[2] to P[0] = 0.38
P[1] sends Q_send[3] to P[0] = 0.38
P[1] receives Q_recv[0] from P[2] = 1.00
P[1] receives Q_recv[1] from P[2] = 1.00
P[1] receives Q_recv[2] from P[2] = 1.00
P[1] receives Q_recv[3] from P[2] = 1.00

P[2] sends Q_send[0] to P[1] = 1.00
P[2] sends Q_send[1] to P[1] = 1.00
P[2] sends Q_send[2] to P[1] = -24.03
P[2] sends Q_send[3] to P[1] = -24.03
P[2] receives Q_recv[0] from P[3] = 1.00
P[2] receives Q_recv[1] from P[3] = 1.00
P[2] receives Q_recv[2] from P[3] = 1.00
P[2] receives Q_recv[3] from P[3] = 1.00

P[3] sends Q_send[0] to P[2] = 7.95
P[3] sends Q_send[1] to P[2] = 7.95
P[3] sends Q_send[2] to P[2] = 0.38
P[3] sends Q_send[3] to P[2] = 0.38
P[3] receives Q_recv[0] from P[0] = -2.12
P[3] receives Q_recv[1] from P[0] = -2.12
P[3] receives Q_recv[2] from P[0] = 4.12
P[3] receives Q_recv[3] from P[0] = 4.12

小提琴: https://jsfiddle.net/otzuc2oq/