[英]JavaScript setInterval only runs once
我試圖讓HTML頁面使用JavaScript的setInterval函數每秒用文本文件中的內容更新文本區域。 但是,setInterval調用內的函數似乎只運行一次。
使用Javascript:
// Send a GET request to the given location
function sendRequest(location, nonblocking) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", location, nonblocking);
xmlhttp.send();
return xmlhttp.responseText;
}
// Refresh the communication log
function refreshLog() {
document.getElementById("comm_log").value = sendRequest("src/log.txt", false);
}
window.setInterval(refreshLog, 1000);
該請求不是異步的,這僅僅是因為文本文件永遠不會太長,而這正是我試圖快速整合在一起的事情。
HTML:
<html>
<head>
<style type="text/css">
textarea {
width: 98%;
height: 80%;
resize: none;
font-family: "Courier New";
}
</style>
<script type="text/javascript" src="src/script.js"></script>
</head>
...
<textarea id="comm_log" readonly></textarea>
...
</html>
有人有主意嗎?
我想您的瀏覽器正在緩存。 您只獲取一次數據,然后從緩存中處理下一個請求。 因此, refreshLog
應該被重復調用,僅以下調用不會對頁面產生任何影響,因為它是從緩存中提供的。
在URL上附加一些獨特的部分,例如?=<timestamp>
。
xmlhttp.open("GET", location + '?=' + new Date().getTime(), nonblocking);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.