Javascript 当涉及异步调用时,如何设置特定的执行顺序?
我是JavaScript世界的新手(2天!!),我之前唯一的编码经验是在Java中,语句的执行是按顺序进行的。Javascript 当涉及异步调用时,如何设置特定的执行顺序?,javascript,asynchronous,callback,Javascript,Asynchronous,Callback,我是JavaScript世界的新手(2天!!),我之前唯一的编码经验是在Java中,语句的执行是按顺序进行的。 我知道,或者至少我读过JavaScript是异步的,这意味着如果有一条语句需要很长时间才能执行,那么执行下一条语句时,不会因为第一条语句而中断程序。 我遇到过回调(实际上很多!!),但我不知道如何使用回调来确定执行顺序。我写了一段代码只是为了了解如何做到这一点,我肯定需要一些帮助 console.log("Beginning"); function Test(callback){
我知道,或者至少我读过JavaScript是异步的,这意味着如果有一条语句需要很长时间才能执行,那么执行下一条语句时,不会因为第一条语句而中断程序。 我遇到过回调(实际上很多!!),但我不知道如何使用回调来确定执行顺序。我写了一段代码只是为了了解如何做到这一点,我肯定需要一些帮助
console.log("Beginning");
function Test(callback){
setTimeout(function(callback){
console.log("Something that takes a lot of time");
},5000);
callback();
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
我想让输出显示-
Beginning
Something that takes a lot of time
Should come last
但我得到的结果是-
Beginning
Should come last
Something that takes a lot of time
我能做些什么来获得我想要的输出吗?将回调放在
setTimeout
内部而不是外部,因为回调将在setTimeout执行之前首先执行,因为javascript不会等待setTimeout
执行()并执行下一行,因此无法获得所需的输出
console.log("Beginning");
function Test(callback){
setTimeout(function(){
console.log("Something that takes a lot of time");
callback();
},5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
console.log("Beginning");
function Test(callback){
console.log("Something that takes a lot of time");
setTimeout(callback,5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
将回调放在setTimeout
内部而不是外部,因为回调将在setTimeout之前首先执行,因为javascript不会等待setTimeout
execution()并执行下一行,因此您将无法获得所需的输出
console.log("Beginning");
function Test(callback){
setTimeout(function(){
console.log("Something that takes a lot of time");
callback();
},5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
console.log("Beginning");
function Test(callback){
console.log("Something that takes a lot of time");
setTimeout(callback,5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
你说的很多话都是错的。JavaScript与Java一样是顺序的,但异步调用更频繁。如果希望在长时间运行后调用回调,则必须在长时间运行的程序后调用回调。像这样-
console.log("Beginning");
function Test(callback){
setTimeout(function(callback){
console.log("Something that takes a lot of time");
callback();
},5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
你说的很多话都是错的。JavaScript与Java一样是顺序的,但异步调用更频繁。如果希望在长时间运行后调用回调,则必须在长时间运行的程序后调用回调。像这样-
console.log("Beginning");
function Test(callback){
setTimeout(function(callback){
console.log("Something that takes a lot of time");
callback();
},5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
我已经修改了您的代码如下,以获得所需的输出
console.log("Beginning");
function Test(callback){
setTimeout(function(){
console.log("Something that takes a lot of time");
callback();
},5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
console.log("Beginning");
function Test(callback){
console.log("Something that takes a lot of time");
setTimeout(callback,5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
setTimeout接受一个回调函数,该函数将在指定的时间间隔后执行
setTimeout的使用是异步部分。当执行上述代码时,首先打印“Begining”控制台语句,然后调用测试函数,传入一个需要在500毫秒后异步执行的函数。我修改了您的代码,如下所示,以获得所需的输出
console.log("Beginning");
function Test(callback){
setTimeout(function(){
console.log("Something that takes a lot of time");
callback();
},5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
console.log("Beginning");
function Test(callback){
console.log("Something that takes a lot of time");
setTimeout(callback,5000);
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
setTimeout接受一个回调函数,该函数将在指定的时间间隔后执行
setTimeout的使用是异步部分。执行上述代码时,首先打印“Begining”控制台语句,然后调用测试函数,传入一个需要在500毫秒后异步执行的函数。让我们澄清一下您所说的:
我是JavaScript世界的新手(2天!!),也是我之前唯一的一个代码
经验是在Java中执行语句
按顺序。我理解这一点,或者至少我读过JavaScript
是异步的,这意味着如果有一个语句
执行时间长,执行下一条语句时不保留
为第一个语句启动程序
这不是它的工作原理。给定的函数在设计上可以是异步的,也可以是同步的。它与执行所需的时间完全无关。您可以有一个非常快速的异步函数或一个非常长的同步函数。决定函数是否异步的是它的设计方式。如果它使用异步I/O或计时器或任何其他异步基础结构,那么至少部分函数的执行是异步的。这意味着一些函数将在稍后完成,而此函数调用之后的一些代码将在异步部分完成之前执行
我遇到过回调(实际上很多!!),但我看不出它们是怎么回事
可用于确定执行顺序。我写了一篇
代码只是为了理解如何实现它,我当然可以使用一些
救命啊
回调用于在某些异步操作完成时通知调用代码。这可以用于使用异步操作的结果,也可以用于执行希望在异步操作完成后按顺序运行的下一段代码
在代码示例中,如果需要所需的序列,则必须在setTimeout()
回调中调用回调,以便在执行setTimeout()
调用后调用它,从而获得所需的序列
您还必须删除setTimeout
回调的callback
参数。该回调函数没有与该参数一起传递,因此在那里声明它是错误的。可以通过闭包直接从父函数访问它,如下所示:
console.log("Beginning");
function Test(callback){
setTimeout(function(){
console.log("Something that is asynchronous");
// call the callback here to indicate to the calling code
// that the asynchronous operation is now complete
callback();
},5000);
console.log("After Setting Timer");
}
function tstCallBack(){
console.log("Should come last");
}
Test(tstCallBack);
这将在控制台中生成一个序列:
开始
设置定时器后
异步的东西
应该排在最后
从概念上讲,Javascript引擎运行一个线程,该线程使用一个事件队列。在上面的函数中,就是这样
第一个console.log(“开始”)代码>被执行
调用测试(tstCallback)
作为执行Test()
函数的一部分,会安排一个计时器。这将在JS引擎内部注册一个计时器
继续执行Test()
中的代码,console.log(“设置计时器后”)代码>被执行,然后该函数完成
JS执行的当前线程完成,如果事件队列中没有其他内容,则JS引擎无事可做,但等待下一个事件发生
一段时间后(计时器设置为5秒),内部计时器启动,并将计时器事件置于