13

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

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

我的问题:

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

在这种情况下可能是什么情况:

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

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

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

4

4 回答 4

15

不,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
})
于 2012-04-21T10:13:33.930 回答
2

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

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

function doStuff(callback) {
    fillTree();

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

doStuff(checkSelectedBoxes);
于 2012-04-21T10:18:00.393 回答
0

setTimeout函数不等待执行,您可以在以下代码段中清楚地检查它。您可以看到,在给定waitTimes随机时间数组的情况下,该Array.map函数将首先打印出所有time[index]=waitTimes[index]值,然后wait[index]=waitTimes[index]在毫秒setTimeout后准确地触发。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" />

于 2016-04-17T22:36:50.360 回答
0

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

<!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 使当前线程进入一个有效的等待状态,至少是你请求的时间量(然后它就有资格再次被调度)。

如果您不想接近准确时间,则无需检查clock()。”

-- pthread休眠功能,cpu消耗

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

于 2016-08-15T05:05:47.910 回答