gpt4 book ai didi

javascript - Javascript setTimeout是否停止其他脚本执行

转载 作者:可可西里 更新时间:2023-11-01 02:05:56 24 4
gpt4 key购买 nike

我指的是 this .一切都还不清楚。

  • 我有一个 JS 函数 fillTree() 可以更新一棵树,它有复选框。
  • 我有另一个函数 checkSelectedBoxes(),它在 window.onload 上执行,检查选中的复选框。
  • 现在连接了很多其他功能。

我的问题:

  • 如果我正在使用 setTimeout(),其他脚本函数是否也会停止并等待我的函数完成加载?

这可能是什么情况:

function fillTree(){...}
function checkSelectedBoxes(){...}

fillTree(); // This take time to get data. onLoad() doesnt work.
setTimeout(function(){ checkSelectedBoxes() },5000);

即使在增加时间间隔后,这也会返回空值。 fillTree() 是否暂停执行?

最佳答案

不,setTimeout 不会等你(因此,JS 没有暂停功能)。 setTimeout 所做的是稍后搁置该任务,并允许执行下一行。当达到该超时时,它将该任务插入到执行行中。并且当没有其他代码在运行时,它会执行指示的函数。

您想要做的是对您的 fillTree() 进行回调并在完成时执行。

function fillTree(callback){

//do something very long

callback(); //execute passed function when it's done
}

fillTree(function(){
checkSelectedBoxes(); //executes only when fillTree finishes
})

关于javascript - Javascript setTimeout是否停止其他脚本执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258012/

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