所以現在我不斷地用GET發送xmlhttprequests到一個PHP腳本,這個腳本讓我回到文件夾中的文件數量。如何連續檢查AJAX和PHP文件夾中有多少文件
我用setInterval()
重複了javascript函數,它工作得很好,但我希望setInteral()
一旦從我的PHP腳本中取回某個數字就會停止。
這裏是我的代碼:
<script>
function checkmedia(url,format) {
var format1 = format;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
progress = this.responseText;
document.getElementById("progress").innerHTML =
this.responseText;
}
};
xhttp.open("GET", 'checkfilecount.php?userurl='+url+'&act=run&format-option=' + format, true);
xhttp.send();
if(progress != "100") {
var media_progress = setInterval(checkmedia.bind(null,url,format1), 10000);
}
}
</script>
由於我continiously調用這個XMLHttpRequest的和多次(一個表)我得到了內存泄漏。
歡迎任何形式的幫助。謝謝。
我認爲這是一個更好的方法,因爲'setInterval'不會等待前一個實例完成處理,如果它需要很長時間,我們可能會有多個實例在運行,導致內存再次泄漏。這是一個可能的情況? –
@BlazeSahlzen - 很明顯,'setTimeout()'版本一次最多保證一個ajax調用,所以我認爲這樣更安全,雖然我只是意識到我簡化了一點,因爲我應該這樣做,因爲我應該把在'onreadstatechange'處理程序中(如果我現在要這樣做),並且我應該允許發生ajax錯誤(我不會爲此煩惱)。即使發生錯誤,至少'setInterval()'也會繼續輪詢。 – nnnnnn
我在這個答案中使用的代碼,它完美的作品!謝謝。儘管如此,兩個答案都非常有幫助,我從他們身上學到了很多東西。謝謝 – userlip