'getJSON caches file while it is being updated
I have a loading process running which fills my database over the course of roughly 37 seconds on my local machine. I am at this point making a process for this, but the results are skewed.
As I am running my code I call getJSON
in a loop to constantly pull data out of a JSON file. While I am making my request, I have my loading perform a constant update on set JSON file as described.
However, it seems my pull is constantly being cached as I get constantly the same results back. If I refresh the JSON file in my browser while doing in the loading procedure, then it suddenly it works correctly again.
I am assuming this is a caching problem, but adding the false
caching to the AJAX variables probably isn't gonna cut it. It's a process that runs next to it. Does anyone know how to force it to pull an uncached file, so I always have the right results?
$.ajax({
cache: false,
url: vars.url,
type: "post",
data: r,
async: true,
processData: vars.process,
contentType: vars.contenttype,
beforeSend: function() {
if (vars.loadbar == 'true') {
interval = setInterval(function() {
$.getJSON(domain + '/core/files/results.json', function(data) {
console.log(data);
})
}, 500);
}
},
complete: function() {
clearInterval(interval);
},
success: function(data) {
// ...
}
});
Solution 1:[1]
I realized I let this question do the running here, but this is how i ended up solving this issue.
beforeSend: function(){
if(vars.loadbar == 'true'){
interval = setInterval(function () {
$.getJSON(domain + '/core/files/results.json?sid=' + Math.random() , function (data) {
// extra code and console logs
})
}, 500);
}
},
In the end I adapted the comment from @AdamRoof to actually use in the secondairy call. Now it wont be cached so I have always the correct value back.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dorvalla |