jspファイル内に次のdivがあります
<div id="myDiv">
<img alt="" src="http://localhost:8080/chartDemo/servlet/ChartDemoServlet">
</div>
一定間隔で画像を自動更新したい。
試してみまし<meta http-equiv="Refresh">
たが、完璧に機能しますが、ページ全体が更新されます。
画像のみを更新するにはどうすればよいですか?
jspファイル内に次のdivがあります
<div id="myDiv">
<img alt="" src="http://localhost:8080/chartDemo/servlet/ChartDemoServlet">
</div>
一定間隔で画像を自動更新したい。
試してみまし<meta http-equiv="Refresh">
たが、完璧に機能しますが、ページ全体が更新されます。
画像のみを更新するにはどうすればよいですか?
一定の間隔でその div を自動的に更新したい。
どんな内容で?それともimg
毎回変わるの?
もしそうなら:
setInterval(function() {
$("#myDiv").html('<img alt="" src="http://localhost:8080/chartDemo/servlet/ChartDemoServlet">');
}, 1000);
img
...のsrc
URLによって返されたデータのキャッシュ ヘッダーが正しい場合は、1 秒に 1 回 (1000ms = 1 秒) 行う必要があります (そうしないと、ブラウザーが以前のバージョンをキャッシュする可能性があります)。の内容を完全に破棄しdiv
、指定されたマークアップをそれに割り当てます。これにより、img
要素が再作成され、再フェッチが発生します (ヘッダーが正しい場合)。
setInterval(function() {
// use to prevent browser cache
var d = new Date();
$("#myDiv img").attr("src", "http://localhost:8080/chartDemo/servlet/ChartDemoServlet?"+d.getTime());
}, 3000); // every 3 seconds.
window.setInterval タイマーを使用する必要があります。その関数では、img src を変更します。このようなもの
//timer function
window.setInterval(refreshImage,1000);
function refreshImage()
{
$('#urdiveid img').attr('src')='new location';
}
<div id="myDiv" onload="JavaScript:timedRefresh(5000);">
<img alt=""
src="http://localhost:8080/chartDemo/servlet/ChartDemoServlet">
</div>
ジャバスクリプトに入れる
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
JavaScriptのタイムアウト機能でできる
var t=setTimeout("functiontoCall()", 3000)
FunctiontoCall()
にコンテンツをロードするために、独自の を記述できますmyDiv
。ajax 呼び出しまたは単純な Dom 操作のいずれかです。