reload/update page javascript
I think that my question is actually very simple and I thought the answer was somewhere hidden in some related questions. But I couldn't get my code to work, so here is my problem:
I need to read a file that is non-periodically chanced and do really nice stuffs with the data in it. So far, I've been doing this "locally" (this file will be on a server开发者_JS百科 later) and it works just fine. Just for testing to see if the data was being read correctly, the changes on the file were done by me and I just hit F5 on the browser to get the "new page". All this is fine!
The thing is, I need the webpage to reload itself only when the file was changed. So I read the file, check if update!= lastupdate to reload the page. The problem is that it doesn't matter if the condition is true or false the page always reloads!! not cool! This is one of the approaches I've done so far:
setInterval(function() {
$.getJSON('object.json', function(data) {
if ( data.update != lastUpdate ){
lastUpdate = data.update;
window.location.reload();
}
});
}, 2000);
This functions checks every 2 seconds if the file was changed and then if true reload the page. But it reloads every 2 seconds instead of each time the file is changed ... Could anyone tell me what am I doing wrong?
Thanks and regards, Julls
Are you missing a closing curly brace?
setInterval(function() {
$.getJSON('object.json', function(data) {
if ( data.update != lastUpdate ){
lastUpdate = data.update;
window.location.reload();
}
});
}, 2000);
精彩评论