这可能吗?现在,我已经完成了实时聊天,在 jquery 的帮助下,我连接到 .php 文件并检查上次修改时间,如果与以前不同,我将检索消息。如果可以在 javascript 中实现,我可能会节省大量资源。
谢谢。
最佳答案
如果服务器为该特定文件发送准确的 Last-Modified header ,这绝对是可能的:
var getMTime = function(url, callback) {
var xhr = XMLHttpRequest();
xhr.open('HEAD', url, true); // use HEAD - we only need the headers
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var mtime = new Date(xhr.getResponseHeader('Last-Modified'));
if (mtime.toString() === 'Invalid Date') {
callback(); // dont want to return a bad date
} else {
callback(mtime);
}
}
}
xhr.send();
};
getMTime('url here', function(mtime) {
if (mtime) console.log('the mtime is:' + mtime.toISOString());
});
关于php - 是否可以使用 javascript 查找文件的最后修改时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5746598/