Javascript 中的调用是同步的吗?
Are calls synchronous in Javascript?
我找不到这个问题的答案,对不起,如果是双post。每当我搜索这个时,我都会收到 AJAX 个问题。那么,first
和 second
调用是同步的吗:
function test() {
first();
second();
}
?
当然不是...只是尝试 console.log 它们内部,它们之间的警报,您应该看到它们是同步的。
只有第一个也是异步的(但这意味着它在开始第二个之前结束),它们才能变得像异步一样。
根据我的问题之后(或同步 :D)提供的示例,我认为这样更好:
function first() {
console.log('1st ');
}
function second() {
console.log('2nd ');
}
function test() {
first();
alert("you see just the first one");
second();
}
只要函数 first()
和 second()
在它们的范围内不是异步的,它们就是同步的。
但是 first()
无论如何都会在 second()
之前被调用。
您可以通过使用 console.log()
将某些内容记录到控制台来使用 fiddle 找到它。
function first() {
console.log('1st ');
}
function second() {
console.log('2nd ');
}
function test() {
first();
second();
}
// -> 1st 2nd
我找不到这个问题的答案,对不起,如果是双post。每当我搜索这个时,我都会收到 AJAX 个问题。那么,first
和 second
调用是同步的吗:
function test() {
first();
second();
}
?
当然不是...只是尝试 console.log 它们内部,它们之间的警报,您应该看到它们是同步的。
只有第一个也是异步的(但这意味着它在开始第二个之前结束),它们才能变得像异步一样。
根据我的问题之后(或同步 :D)提供的示例,我认为这样更好:
function first() {
console.log('1st ');
}
function second() {
console.log('2nd ');
}
function test() {
first();
alert("you see just the first one");
second();
}
只要函数 first()
和 second()
在它们的范围内不是异步的,它们就是同步的。
但是 first()
无论如何都会在 second()
之前被调用。
您可以通过使用 console.log()
将某些内容记录到控制台来使用 fiddle 找到它。
function first() {
console.log('1st ');
}
function second() {
console.log('2nd ');
}
function test() {
first();
second();
}
// -> 1st 2nd