Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the JSON file every n minutes

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?

like image 790
user2798227 Avatar asked Dec 17 '25 13:12

user2798227


1 Answers

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
like image 165
Reinstate Monica Cellio Avatar answered Dec 20 '25 02:12

Reinstate Monica Cellio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!