2012-04-21 57 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

所以我猜這個操作系統並調度給你,此時它中斷髮動機,發動機停止世界,並把超時功能隊列的頂部儘快執行?