我有以下JS代码:
<script>
first();
second();
</script>
我想确保second()
它将在完全执行后运行first()
。那是预期的默认行为吗?
这取决于您first()
和second()
函数内部的内容。.如果您有一些异步调用,则first()
可能在之后second()
。
例如
function first(){
console.log("I'm first");
}
function second(){
console.log("I'm second");
}
first();
second();
将打印
我先
我第二
现在假设您的first()
函数中有一个ajax调用,该调用需要10秒才能结束:
function first(){
$.ajax({
//--- blah blah
success: function(){
//--- success runs after 10 seconds
console.log("I'm first");
}
})
}
如果你跑
first();
second();
您将已经打印
我第二
我先
在这里您可以找到另一个例子
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句