Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript method to import a local JSON file every x amount of minutes

I'm fairly new to JavaScript and I've been hunting around for a solution for this problem:

I have names and numbers being written to a data.json file in the same directory as my JavaScript file. What I'm looking for is every few minutes to check that data.json and update my HTML p tag with the changes.

My HTML block looks like this:

...
<body>
  <p id="mydata">
  </p>
</body>
...

My data.json looks like this:

[{"Name":"Charlie","Number":"5"},{"Name":"Patrick","Number":"3"}]

My Javascript block looks like this:

...
setInterval(function(){
    var json = // read in json file
    //this is the part I'm missing
    document.getElementById('mydata').innerHTML = json;
},300000); // every 5 minutes
like image 744
Cody Avatar asked Nov 07 '22 10:11

Cody


1 Answers

$.getJSON will load local json file

setInterval(function(){
    $.getJSON("yourjsonfile.json", function(json) {
        console.log(json); 
        document.getElementById('mydata').innerHTML = json;
    });
},300000); // every 5 minutes
like image 171
Wesgur Avatar answered Nov 14 '22 21:11

Wesgur