[英]When calling a javascript function within another function, is it executed synchronously?
In the following code: 在下面的代码中:
function doStuffA() {
// DO SOME THINGS
doStuffB();
// DO SOME MORE THINGS
}
function doStuffB() {
// DO B THINGS
}
doStuffA();
What order is the code executed? 代码按什么顺序执行?
Is it: 是吗:
1. DO SOME THINGS
2. DO B THINGS
3. DO SOME MORE THINGS
Or: 要么:
1. DO SOME THINGS
2. DO B THINGS & DO SOME MORE THINGS - AT THE SAME TIME
Assuming you intended for there to be a call to doStuffB
in doStuffA
, and a call to doStuffA
somewhere... 假设你打算在那里是一个电话
doStuffB
在doStuffA
,并呼叫doStuffA
某处...
Javascript is traditionally executed synchronously. 传统上,Javascript是同步执行的。
Therefore: 因此:
1. DO SOME THINGS
2. DO B THINGS
3. DO SOME MORE THINGS
There's been a lot of discussion about your syntax, but I think I can fill in the blanks well enough to understand what you're asking. 关于您的语法有很多讨论,但是我想我可以很好地填补空白,以了解您的要求。 You want to know whether JavaScript is executed sequentially;
您想知道JavaScript是否按顺序执行。 specifically, if a function call will pause execution of the calling function until the called function terminates.
具体来说,如果函数调用将暂停执行调用函数,直到被调用函数终止。
For the general case, the answer is yes. 对于一般情况,答案是肯定的。 Here's some example code to illustrate this:
以下是一些示例代码来说明这一点:
var count = 0;
var result = "";
function a() {
result += "first part of A. \n";
b();
result += "last part of A. ";
}
function b() {
for (var i = 0; i < 100000; i++) {
count++;
}
result += "even after " + count + " cycles, A waits for B. \n";
}
a();
console.log(result);
Runnable version on jsFiddle: http://jsfiddle.net/jmorgan123/574Rh/ jsFiddle上的可运行版本: http : //jsfiddle.net/jmorgan123/574Rh/
Try it out. 试试看。 You'll see that the result is:
您会看到结果是:
first part of A.
even after 100000 cycles, A waits for B.
last part of A.
There are exceptions to this, and they're very important. 有一些例外,它们非常重要。 When you set an interval or timeout, the program starts a timer and plans to run a function when the timer finishes.
当您设置间隔或超时时,程序将启动计时器并计划在计时器结束时运行功能。 But it doesn't wait around for that to happen;
但是,它不会等待这种情况的发生。 once the timer has started, the rest of the program goes on with its other tasks:
计时器启动后,程序的其余部分将继续执行其他任务:
//won't do what you want:
result += "first part of A ";
setInterval(b, 1000);
result += "last part of A ";
In this case, when a()
is finished running, result
will be: 在这种情况下,当
a()
完成运行时, result
将是:
first part of A, last part of A
In fact, that's all you'll see, because console.log(result)
will run before b()
does. 实际上,这就是全部,因为
console.log(result)
将在b()
之前运行。
Another important case where JS defers execution is in AJAX calls; JS推迟执行的另一个重要情况是在AJAX调用中。 this trips people up all the time.
这总是使人们绊倒。 Here's an example of this mistake:
这是此错误的示例:
//also won't do what you want:
result += "first part of A ";
$.get('ajax/test.html', function(data) {
result += "now with AJAX! ";
});
result += "last part of A ";
Again, result will be first part of A, last part of A
when console.log(result)
runs. 同样,当
console.log(result)
运行时,结果将是first part of A, last part of A
。 The only way to guarantee your code runs after the AJAX call is by putting it in the callback: 保证代码在 AJAX调用后运行的唯一方法是将其放入回调中:
var result = "";
function a() {
result += "first part of A ";
$.get('ajax/test.html', function(data) {
result += "now with AJAX! ";
result += "last part of A ";
console.log(result);
});
}
a();
alert()
and confirm()
alert()
和confirm()
One final note: Interestingly, the functions alert()
and confirm()
do , in fact, interrupt control flow. 最后一点:有趣的是,函数
alert()
和confirm()
确实中断了控制流。 If your code looks like this: 如果您的代码如下所示:
result += "first part of A, ";
result += confirm("what is your choice?"); //let's assume you click 'OK' here
result += ", last part of A";
...the result will be first part of A, true, last part of A
no matter how long you wait to click OK. ...无论等待多长时间单击“确定”,结果都将是
first part of A, true, last part of A
。 confirm
and alert
are the only cases (correct me if I'm wrong) where JavaScript will pause execution while it waits for some outside source. confirm
和alert
是唯一的情况(如果我错了,请纠正我),在这种情况下,JavaScript将在等待某些外部源时暂停执行。
Okay, assuming you want to call doStuffA()
... 好吧,假设您要调用
doStuffA()
...
function doStuffA() {
// DO SOME THINGS
doStuffB();
// DO SOME MORE THINGS
}
function doStuffB() {
// DO B THINGS
}
The above will result in 以上将导致
1. DO SOME THINGS
2. DO B THINGS
3. DO SOME MORE THINGS
If you want the asynchronous approach you would use setTimeout()
. 如果要使用异步方法,则可以使用
setTimeout()
。 Here are some docs: https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout 以下是一些文档: https : //developer.mozilla.org/zh-CN/docs/DOM/window.setTimeout
ie 即
function doStuffA() {
// DO SOME THINGS
setTimeout(doStuffB,1);
// DO SOME MORE THINGS
}
function doStuffB() {
// DO B THINGS
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.