Javascript setTimeout 会停止其他脚本执行吗?

Does Javascript setTimeout stop other script execution

本文关键字:脚本 执行 其他 setTimeout Javascript      更新时间:2023-09-26

我指的是这个。一切还不清楚。

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

我的问题:

  • 如果我正在使用setTimeout()另一个脚本函数也会停止并等待我的函数完成加载吗?

这可能是什么情况:

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

即使在增加时间间隔后,这也会返回 null 值。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
})

这是我正在使用的CMS,并且树设置在其他JS文件中,我不会干扰它,因为它使用了许多其他功能,并且情况可能并不总是相同的

如果无法修改fillTree()函数,则可以将其包装在自己的函数中,并对其应用回调函数。试试这个:

function doStuff(callback) {
    fillTree();
    // Call the callback parameter (checkSelectedBoxes() in this case)
    // when fillTree() has completed
    callback();
}
doStuff(checkSelectedBoxes);

setTimeout 函数不会等待执行,您可以在以下代码片段中清楚地检查它。您可以看到,给定随机时间的数组waitTimesArray.map 函数将首先打印出所有time[index]=waitTimes[index]值,然后在 waitTimes[index] 毫秒后触发setTimeout时打印出wait[index]=waitTimes[index]

var console = {
  log: function(s) {
    document.getElementById("console").innerHTML += s + "<br/>"
  }
}
var roundDecimals = function(num, pos) {
  pos = pos || 4;
  return (Math.round(num * Math.pow(10, pos)) / Math.pow(10, pos));
}
var arrayRangeMap = function(a, block) {
  c = [];
  while (a--) c[a] = block();
  return c
};
var getRandomArbitrary = function(min, max) {
    return (Math.random() * (max - min) + min);
  }
  // random 10 wait times, 3 decimal positions
var waitTimes = arrayRangeMap(10, function() {
  return roundDecimals(getRandomArbitrary(0.250, 0.5, 3) * 1000, 2);
});
waitTimes.map(function(item, index) {
  setTimeout(function() {
    console.log("wait[" + index + "]=" + item);
  }, item);
  console.log("time[" + index + "]=" + item);
});
<div id="console" />

我刚刚做了一个可能感兴趣的测试:

<!DOCTYPE html>
<html>
  <head>
    <script>
/* test: if returns in around 1000ms, must interrupt, otherwise it is indeed
   not going to let timeout work until work is done. */    
window.onload = function() {
    var before = performance.now();
    setTimeout(function() {
        alert('completed in ~' + (performance.now() - before) + ' nanoseconds.');
    }, 1000);
    /* count to 2^31 - 1... takes 8 seconds on my computer */
    for (var i = 0; i < 0xffffffff; ++i) {
        /* me busy, leave me alone */ 
    }
};      
    </script>
  </head>
  <body>
  </body>
</html>

我现在真的很好奇 JavaScript 是如何知道何时经过一定时间的。它会生成一个休眠一段时间的线程吗?如果是这样,睡眠线程的数量是否有限制,或者休眠线程是否"存储"直到需要它们?很困惑。

我认为这与

"如果它不需要正好是1s,那么就睡一秒钟。 usleep 和 sleep 将当前线程置于高效等待状态 这至少是您请求的时间量(然后它变成了 有资格再次安排)。

如果您不想接近确切的时间,则无需检查 时钟()."

-

-线程休眠功能,CPU消耗

所以我想操作系统会为你做调度,此时它会中断引擎,引擎会停止世界并将超时功能放在队列之上以尽快执行?