Get the JSON file every n minutes

ajax, jquery, json, refresh

Solution

Wrap your code in a function and then it can use `setTimeout` to run itself again 5 minutes later...

function getData() {
    $.getJSON("new_json_file.json", function (data) {
        $.each(data, function (i, item) {
            //Here I am working on the data
        });
        setTimeout(getData, 300000);
    });
}
getData(); // run once to start it

Problem

I have written code which gets the JSON file and work on it. But that file gets updated every 5 minutes. So I want my code to get the fresh data without causing refresh. Here is how I am getting the JSON file. ``` $.getJSON("new_json_file.json", function (data) { $.each(data, function (i, item) { //Here I am working on the data }); }); ``` Can anyone help?

Original source