【发布时间】:2021-09-03 23:23:51
【问题描述】:
let fn_one = () => {
setTimeout(function() {
console.log("This is shashank");
}, 5000)
}
let fn_two = (name, callback) => {
setTimeout(function() {
console.log("Hi!, " + name);
callback();
}, 1000)
}
fn_two("there", fn_one)
这里,fn_two 被调用并在一秒后执行,然而,它有一个回调函数被设置为fn_one()。所以它应该在执行fn_two函数后立即调用回调函数,对吧?为什么在调用 fn_two 后需要 5 秒?为什么它不能在后台运行?
任何人都可以向我解释为什么函数 fn_one 需要 5 秒,即使我已将其调用为回调函数?
【问题讨论】:
-
因为
fn_one使用setTimeout(..., 5000)在5秒后安排console.log()? -
fn_one 会立即被
callback()调用。如果你把console.log('fn_one called');放在 fn_one 的 setTimeout 之前,你应该会看到这个。
标签: javascript html asynchronous callback asynccallback