【发布时间】:2017-05-21 00:09:27
【问题描述】:
前面所有函数都完成后如何调用第二个函数。
function first() {
// code here
setTimeout( function() {
// code here
}, 1000);
// code here
setTimeout( function() {
// code here
}, 3000);
// code here
setTimeout( function() {
// code here
}, 3800);
}
function second() {
// code here
}
first();
first();
second();
first();
second();
似乎所有功能都在同一时间执行。
非常感谢。
【问题讨论】:
-
基本上每次调用都需要增加超时时间(毫秒),否则都是同时触发的。
-
调用 settimeouts 是异步的。他们不会互相等待
-
根据条件调用的函数,所以不知道需要的超时时间。
-
如果你需要在
first异步方法之后运行second函数,你可以将first包装在一个promise中,并在promise解决时在then链中调用second。
标签: javascript jquery settimeout