Javascript setTimeout是否会停止其他脚本执行 [英] Does Javascript setTimeout stop other script execution
问题描述
我指的是这个。一切都还不清楚。
I am referring to this. Everything is still not clear.
- 我有一个JS函数
fillTree()
哪个更新树,它有复选框。 - 我有另一个函数
checkSelectedBoxes()
在窗口上执行.onload
检查选中的复选框。 - 现在连接了很多其他功能。
- I have a JS function
fillTree()
which updates a tree, it has checkboxes. - I have another function
checkSelectedBoxes()
which is executed onwindow.onload
which checks for selected checkboxes. - Now there are lots of other functions connected.
我的问题:
- 如果我使用
setTimeout()
其他脚本函数是否也会停止并等待我的函数完成加载?
- If I am using
setTimeout()
will the other script function also stop and wait for my function to finish loading?
这可能是这种情况:
function fillTree(){...}
function checkSelectedBoxes(){...}
fillTree(); // This take time to get data. onLoad() doesnt work.
setTimeout(function(){ checkSelectedBoxes() },5000);
即使在增加时间间隔后,这也会返回空值。 fillTree()
暂停执行吗?
This returns me null values even after increasing the time interval. Does fillTree()
pause execution?
推荐答案
不, setTimeout
不等你(因此,JS没有暂停功能)。什么 setTimeout
确实在以后留出该任务,并允许执行下一行。当超时达到时,它会将该任务插入执行行。当没有其他代码正在运行时,它会执行指示的函数。
No, setTimeout
does not wait for you (hence, JS has no pause function). What setTimeout
does is set aside that task at a later time, and allow the next line to be executed. when that timeout is reached , it inserts that task into the execution line. and when no other code is running, it executes the function that was indicated.
你想要做的是回复你的 fillTree()
并在完成后执行。
what you want to do is give a callback to your fillTree()
and execute when it's done.
function fillTree(callback){
//do something very long
callback(); //execute passed function when it's done
}
fillTree(function(){
checkSelectedBoxes(); //executes only when fillTree finishes
})
这篇关于Javascript setTimeout是否会停止其他脚本执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!