gpt4 book ai didi

javascript - 如何有序执行过多的函数? JavaScript

转载 作者:行者123 更新时间:2023-12-01 01:38:12 25 4
gpt4 key购买 nike

示例:

 function first()  
{
alert('FIRST');
}

function second()
{
alert('SECOND');
}

function third()
{
alert('THIRD');
}

我想在第一个函数完成后依次执行所有这个函数。

我使用了setTimeout,但这也会出现错误,具体取决于每个函数的处理方式。

例如,如果我在第二个和第三个函数中设置setTimeout,但是如果第一个函数包含怎么办某种无法立即执行的代码?

请帮助我。

最佳答案

最简单的方法就是这样。

 function first()  
{
alert('FIRST');
this.second(); // Call second function when first one is done...
}

function second()
{
alert('SECOND');
this.third(); // Call third when second function is done...
}

function third()
{
alert('THIRD');
}

first(); // This will execute first...

除了这种方式之外,你还有很多按顺序调用函数的方式。

您可以使用,

1) 异步/等待,

2)回调,

3) promise (bluebird, promise )

关于javascript - 如何有序执行过多的函数? JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52638535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com