使用javascript获取文件的修改时间戳 [英] Get the modified timestamp of a file with javascript

查看:141
本文介绍了使用javascript获取文件的修改时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用JavaScript获取文件的修改时间戳?



我使用JSON文件通过javascript填充页面我希望显示该JSON文件的时间戳。

解决方案

如果你通过真正的ajax检索文件,你可以这样做(也就是说,通过 XMLHttpRequest ),前提是您将服务器配置为在发送数据时发送 Last-Modified 标头。



这里最基本的是当你使用 XMLHttpRequest 时,你可以访问响应头。因此,如果服务器发回 Last-Modified ,您可以使用它:

  var xhr = $ .ajax({
url:data.json,
成功:函数(响应){
display(Data is+ response.data +,最后修改:+ xhr.getResponseHeader(Last-Modified));
}
});

刚试过Chrome,Firefox,IE8和IE11。工作得很好(即使数据来自缓存)。






你在下面说过你需要这样做在循环中,但你不断看到变量的最后一个值。这告诉我你做过这样的事情:

  // **错误** 
var list = / * ...一些URL列表...... * /;
var index;
for(index = 0; index< list.length; ++ index){
var xhr = $ .ajax({
url:list [index],
成功:function(response){
display(Data is+ response.data +,last last modified:+ xhr.getResponseHeader(Last-Modified));
}
} );
}

问题在于所有成功回调对 xhr 变量有持久引用,并且只有其中一个。因此,所有回调都会看到分配给 xhr 的最后一个值。



这是典型的闭包问题。这是一个解决方案:

  var list = /*...some URL列表... * /; 
list.forEach(function(url){
var xhr = $ .ajax({
url:url,
success:function(response){
display( + url +的数据是+ response.data +,最后修改:+ xhr.getResponseHeader(Last-Modified));
}
});
});

因为 forEach 回调的每次迭代都会得到它自己的 xhr 变量,没有串扰。 (您需要在旧浏览器上对 forEach 进行填充。)






<你在下面说过:


我已经考虑过闭包问题,这就是为什么我使用数组 xhr [ e] 在我的循环中 e ...
但是你的例子是doesent help ...


并在一个要点中链接到此代码:

  //循环过e .... 
nodename = arr [e];
node_json = path_to_node_json + nodename;
html + ='< a href ='+ node_json +'target =_ blankid =host _'+ nodename +'> data< / a>< / td>'
+ '< / TR>';
xhr [e] = $ .ajax({
url:node_json,
成功:函数(响应){
$('#host _'+ nodename).append(最后修改:+ xhr [e] .getResponseHeader(Last-Modified));
}
});

仍有经典错误:您的成功函数关闭变量 e ,而不是 success 函数时的值已创建,所以当 success 函数运行时, e 在循环中分配了它的最后一个值。



我之前给出的 forEach 示例非常适合:

  //(我假设`node_json`,`html`和`path_to_node_json`都在函数外部声明为
//。)
arr.forEach(function(nodename){
var xhr; //< ===这个特定调用迭代
//函数的局部变量,后续迭代$ b不会改变值$ b node_json = path_to_node_json + nodename;
html + ='< a href ='+ node_json +'target =_ blankid =host _'+ nodename +'> data< / a>< / td>'
+'< / tr>';
xhr = $ .ajax({
url:node_json,
成功:函数(响应){
//注意:你没有在这里使用它,但只是为了强调:如果
//你使用了` node_json`这里,它的值为
//循环的* end *,因为它不是
//函数的本地值。但是`xhr`是本地的,所以它在
//后续迭代中没有改变。
$('#host _'+ nodename).append(last modified:+ xhr.getResponseHeader(Last-Modified));
}
});
});


Is it possible to get the modified timestamp of a file using just JavaScript?

I use a JSON file to fill a page by javascript and I would like to show the timestamp of that JSON file.

解决方案

You can do it if you're retrieving the file through true ajax (that is, through XMLHttpRequest), provided you configure your server to send the Last-Modified header when sending the data.

The fundamental thing here is that when you use XMLHttpRequest, you can access the response headers. So if the server sends back Last-Modified, you can use it:

var xhr = $.ajax({
    url: "data.json",
    success: function(response) {
        display("Data is " + response.data + ", last modified: " + xhr.getResponseHeader("Last-Modified"));
    }
});

Just tried that on Chrome, Firefox, IE8, and IE11. Worked well (even when the data was coming from cache).


You've said below that you need to do this in a loop, but you keep seeing the last value of the variable. That tells me you've done something like this:

// **WRONG**
var list = /*...some list of URLs...*/;
var index;
for (index = 0; index < list.length; ++index) {
    var xhr = $.ajax({
        url: list[index],
        success: function(response) {
            display("Data is " + response.data + ", last modified: " + xhr.getResponseHeader("Last-Modified"));
        }
    });
}

The problem there is that all of the success callbacks have an enduring reference to the xhr variable, and there is only one of them. So all the callbacks see the last value assigned to xhr.

This is the classic closure problem. Here's one solution:

var list = /*...some list of URLs...*/;
list.forEach(function(url) {
    var xhr = $.ajax({
        url: url,
        success: function(response) {
            display("Data for " + url + " is " + response.data + ", last modified: " + xhr.getResponseHeader("Last-Modified"));
        }
    });
});

Since each iteration of the forEach callback gets its own xhr variable, there's no cross-talk. (You'll need to shim forEach on old browsers.)


You said below:

I already thought about a closure problem, thats why I used an array xhr[e] in my loop over e... But your example doesent help...

and linked to this code in a gist:

//loop over e....
nodename=arr[e];
node_json=path_to_node_json+nodename;
html +='<a href="'+node_json+'" target="_blank" id="host_'+nodename+'">data</a></td>'
    +'</tr>';
xhr[e] = $.ajax({
    url: node_json,
    success: function(response) {
        $('#host_'+nodename).append("last modified: " + xhr[e].getResponseHeader("Last-Modified"));
    }
});

That still has the classic error: Your success function closes over the variable e, not the value it had when the success function was created, and so by the time the success function runs, e has the last value assigned to it in the loop.

The forEach example I gave earlier fits this perfectly:

// (I assume `node_json`, `html`, and `path_to_node_json` are all declared
// here, outside the function.)
arr.forEach(function(nodename) {
    var xhr; // <=== Local variable in this specific call to the iteration
             // function, value isn't changed by subsequent iterations
    node_json=path_to_node_json+nodename;
    html +='<a href="'+node_json+'" target="_blank" id="host_'+nodename+'">data</a></td>'
        +'</tr>';
    xhr = $.ajax({
        url: node_json,
        success: function(response) {
            // Note: You haven't used it here, but just to emphasize: If
            // you used `node_json` here, it would have its value as of
            // the *end* of the loop, because it's not local to this
            // function. But `xhr` is local, and so it isn't changed on
            // subsequent iterations.
            $('#host_'+nodename).append("last modified: " + xhr.getResponseHeader("Last-Modified"));
        }
    });
});

这篇关于使用javascript获取文件的修改时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆