我想一次文件需要多長時間使用它來下載一個HTTPRequest像這樣:的setInterval 1毫秒似乎並沒有實際上是1ms的
function getFile() {
'use strict';
var url = "data.bin";
var rawFile = new XMLHttpRequest();
var timer_var = setInterval(theTimer, 1);
rawFile.open("GET", url, true);
rawFile.onreadystatechange = function() {
if(rawFile.readyState === XMLHttpRequest.DONE && rawFile.status === 200) {
toLog(rawFile.responseText);
window.clearInterval(timer_var);
toLog("Milliseconds for download: " + time_taken);
}
};
rawFile.send(null);
}
function theTimer() {
'use strict';
toLog(time_taken);
time_taken++;
}
正如你可以看到我已經setInterval
打電話theTimer
每一毫秒。所有thetimer()
確實增加了一個變量,理論上這個變量應該有一個以毫秒爲單位的時間間隔運行的值。
當文件被下載時,我輸出數據,清除計時器並顯示以毫秒爲單位的時間。但是,價值不加起來。我應該差不多2秒鐘,但只能在250ms左右。
爲什麼setInterval
真的不是每1ms?
可能重複[什麼是setTimeout的最小毫秒值?](http://stackoverflow.com/questions/9647215/what-is-minimum-millisecond-value-of-settimeout) –
如果你想知道如何花了很長時間,比較時間戳。 'setInterval'和家人在給定的延遲之後的某個時候發射,當它有機會的時候。 –
嗨,亞歷克斯,時間戳!當然。 –