至少有三种方法可以实现这一点。
纯HTML
正如指出的Amitd的评论,在 “show.html” 以下<meta>
标记添加到您的文档的<head>
元素:
<meta http-equiv="refresh" content="5" />
这会自动刷新每5秒钟一页。调整content
属性的值为所需的秒数。
纯JavaScript:
正如MeNoMore指出的那样,当你把它document.location.reload()
将刷新页面。
<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请求中指出可以使用,但你需要编写一个Web服务的URL返回所需的图像。JavaScript的做一个AJAX请求将是这个样子:
<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呼叫。 jQuery是一个JavaScript库,它使得AJAX调用和DOM操作变得更加简单。要使用jQuery库,你需要在你的<head>
元素(作为例子1.4.2版本)对它的引用:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
你也可以下载“jquery.min.js”并在本地托管它,但当然,这只会更改您加载脚本的url。
上面的AJAX功能,当使用jQuery会看起来更像这样写:
<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路线。如果图片网址是静态的,我可能会使用<meta>
刷新标记。
但你已经当刷新图像SRC(每5秒)告诉浏览器。您想在哪个事件中刷新它? – devnull69
您的意思是说多个用户上传图片,每个用户每次都必须查看最新的图片? – enenen
还有meta标签 http://www.webmonkey.com/2010/02/refresh_a_page_using_meta_tags/ – Amitd