0

Need help updating a variable with setInterval Ajax calls: The json object that I am receiving looks like:

[{"percentage":77}]

Here is my code:

function test() {
 var json = (function () {
    var json = null;
    $.ajax({
        'async': false,
        'global': false,
        'url': 'http://10.100.9.22:5000/loadavg',
        'dataType': "json",
        'success': function (data) {
            json = data;
        }
    });
    return json;
})()};


setInterval(test,5000);

I return the object with:

console.log(json[0].percentage)

But I get this error:

ReferenceError: json is not defined

I do know that console.log() is happening before the ajax call and that is why it is undefined but I'm not sure how to fix it, and I'm sure there is a better/easier way?

Thank you

pm1391
  • 275
  • 2
  • 14

0 Answers0