![](/img/trans.png)
[英]Re-writing a JavaScript variable to HTML5 canvas when the value of the variable has been updated
[英]Execute JavaScript if HTML file has been updated 2
從此頁面我得到了一個很好的答案,請參見下面的代碼。 我遇到一個問題。
該腳本正在檢查自身。 該代碼位於index.html中,它將檢查index.html上是否有更改。 如果有更改,它應該刷新自己。 一切正常,但保持不變。
因此,如果您一次更改文件,它將循環顯示“該站點已被修改”並刷新。 有人知道如何解決嗎?
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
var checkUrl="index.html";
var firstCheck = false;
window.setInterval("checkForUpdate()", 1000);
function checkForUpdate() {
$.ajax(checkUrl, {
ifModified : true,
type : 'HEAD',
success : function (response) {
if(firstCheck === false) {
firstCheck = true;
return;
}
$('#output').html('the site has been modified');
}
});
}
</script>
</head>
<body>
<div id="output">Not Modified</div>
</body>
</html>
您的代碼源承認它不起作用,並提供了替代解決方案:
var checkUrl="test.txt";
window.setInterval("checkForUpdate()", 1000);
var pageLoad = new Date().getTime();
function checkForUpdate() {
$.ajax(checkUrl, {
type : 'HEAD',
success : function (response, status, xhr) {
if(firstCheck === false) {
firstCheck = true;
return;
}
// if the server omits the 'Last-Modified' header
// the following line will return 0. meaning that
// has not updated. you may refine this behaviour...
var lastModified = new Date(xhr.getResponseHeader('Last-Modified'))
.getTime();
if(lastModified > pageLoad) {
$('#output').html('the site has been modified');
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.