2012-04-21 53 views
8

我指的是this。一切都还不清楚。Javascript setTimeout是否停止其他脚本执行

  • 我有一个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()暂停执行?

+0

不,“setTimeout”不会暂停执行其他代码。如果在fillTree()完成时试图调用checkSelectedBoxes(),为什么不把它作为回调参数传递,或者只是在'fillTree()'的末尾传递呢? – 2012-04-21 10:13:00

+0

@RoryMcCrossan谢谢,你的答案似乎是最好的解决方案,但它的CMS我正在使用和树设置在其他js文件,我不会干涉,因为它使用了许多其他功能和情况可能并不总是相同 – 2012-04-21 10:15:16

回答

9

不,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 
}) 
+0

谢谢。这清除了一切。我会在8分钟内接受:P。此外,这个问题并没有这样说,但是你能否告诉我这样一个情况:我该怎么做? – 2012-04-21 10:17:16

+0

fillTree究竟做了什么?这是一个Ajax调用? DOM行走?什么需要这么久? – Joseph 2012-04-21 10:18:26

+3

“将该任务插入执行行”并不完全准确。它不会中断用户代码也不会同时运行;它只会在没有JS用户代码正在运行并且达到超时时运行。 – Lucero 2012-04-21 10:19:19

1

它我使用和树在一些其他的js文件,我不跟,因为它使用许多其他功能干扰设置和情况可能不总是同一个CMS

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

function doStuff(callback) { 
    fillTree(); 

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

doStuff(checkSelectedBoxes); 
+0

你的回答帮助了我的功能,但我的问题的答案是由@joseph给出的。谢谢! – 2012-04-21 10:29:33

0

setTimeout函数不会等待执行,你可以清楚地检查它下面的代码片段。您可以看到,给定随机时间的数组waitTimesArray.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" />

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如何知道什么时候的时候一定量已过去。它是否会产生睡眠一段时间的线程?如果是这样,是否有多少线程睡眠的限制,或睡眠线程“存储”,直到需要它们?很混乱。

我认为这是与

“如果它并不需要准确地1秒,然后就usleep第二。 usleep和睡眠把当前线程成一个高效的等待状态 即至少是您请求的时间量(然后它就变成 可以再次安排)

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

- pthread sleep function, cpu consumption

所以我猜这个操作系统并调度给你,此时它中断发动机,发动机停止世界,并把超时功能队列的顶部尽快执行?