ピュアJavaScriptを:
としては、あなたがそれを呼び出すときdocument.location.reload()
ページを更新し、MeNoMoreによって指摘します。
<script type="text/javascript">
//put this somewhere in "show.html"
//using window onload event to run function
//so function runs after all content has been loaded.
//After refresh this entire script will run again.
window.onload = function() {
'use strict';
var millisecondsBeforeRefresh = 5000; //Adjust time here
window.setTimeout(function() {
//refresh the entire document
document.location.reload();
}, millisecondsBeforeRefresh);
};
</script>
そしてtpower AJAXリクエストによって指摘されたように使用することができますが、あなたが希望する画像のURLを返すために、Webサービスを記述する必要があると思います。
<script type="text/javascript">
//put this somewhere in "show.html"
//using window onload event to run function
//so function runs after all content has been loaded.
window.onload = function() {
'use strict';
var xhr,
millisecondsBeforeNewImg = 5000; // Adjust time here
if (window.XMLHttpRequest) {
// Mozilla, Safari, ...
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// IE
try {
// try the newer ActiveXObject
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
// newer failed, try the older one
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// log error to browser console
console.log(e);
}
}
}
if (!xhr) {
// log error to browser console
console.log('Giving up :(Cannot create an XMLHTTP instance');
}
xhr.onreadystatechange = function() {
var img;
// process the server response
if (xhr.readyState === 4) {
// everything is good, the response is received
if (xhr.status === 200) {
// perfect!
// assuming the responseText contains the new url to the image...
// get the img
img = document.getElementById('theImgId');
//set the new src
img.src = xhr.responseText;
} else {
// there was a problem with the request,
// for example the response may contain a 404 (Not Found)
// or 500 (Internal Server Error) response code
console.log(xhr.status);
}
} else {
// still not ready
// could do something here, but it's not necessary
// included strictly for example purposes
}
};
// Using setInterval to run every X milliseconds
window.setInterval(function() {
xhr.open('GET', 'http://www.myDomain.com/someServiceToReturnURLtoDesiredImage', true);
xhr.send(null);
}, millisecondsBeforeNewImg)
};
</script>
その他の方法:最後に
、tpowerの答えにあなたの質問に答えるために... $.ajax()
がするjQueryを使用しているAJAX要求を行うためのJavaScriptは、このようになりますAJAX呼び出し。 jQueryはAJAX呼び出しとDOM操作をより簡単にするJavaScriptライブラリです。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
ます。また、ダウンロードできる「jquery.min.js」:jQueryライブラリを使用するには、あなたの<head>
要素(例として使用されたバージョン1.4.2)で、それへの参照を含める必要があるだろう代わりにそれをローカルにホストしますが、もちろん、スクリプトをロードするURLを変更するだけです。以上のようになりますjQueryのを使用して書かれた
上記のAJAX機能
、: <script type="text/javascript">
//put this somewhere in "show.html"
//document.ready takes the place of window.onload
$(document).ready(function() {
'use strict';
var millisecondsBeforeNewImg = 5000; // Adjust time here
window.setInterval(function() {
$.ajax({
"url": "http://www.myDomain.com/someServiceToReturnURLtoDesiredImage",
"error": function (jqXHR, textStatus, errorThrown) {
// log error to browser console
console.log(textStatus + ': ' + errorThrown);
},
"success": function (data, textStatus, jqXHR) {
//get the img and assign the new src
$('#theImgId').attr('src', data);
}
});
}, millisecondsBeforeNewImg);
});
</script>
私は明らかであると思いとして、jQueryのバージョンがはるかに簡単かつきれいです。しかし、プロジェクトの規模が小さいため、jQueryのオーバーヘッドを気にする必要があるかどうかは分かりません(それだけではありません)。また、あなたのプロジェクト要件がjQueryの可能性を許しているかどうかは分かりません。私はあなたの質問に答えるためにこの例を単に含めました。$.ajax()
は何ですか。
イメージをリフレッシュするための方法が他にもあることは間違いありません。個人的には、画像のURLが常に変化している場合は、AJAXルートを使用します。画像のURLが静的な場合は、おそらく<meta>
の更新タグを使用します。
しかし、イメージsrc(5秒ごと)をリフレッシュするときは、すでにブラウザに伝えています。代わりにそれをリフレッシュしたいイベントはありますか? – devnull69
複数のユーザーが画像をアップロードしており、毎回新しい画像を表示する必要がありますか? – enenen
メタタグ http://www.webmonkey.com/2010/02/refresh_a_page_using_meta_tags/ – Amitd