I am trying to refresh an if
statement inside a function but its giving me an error
cannont read property of status
$.getJSON("<url>/clients.json", function(data) {
var tr;
for (var i = 0; i < data.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + data[i].client + "</td>");
$('table').append(tr);
var refresh = function() {
if (data[i].status == "up") {
$('td', tr).css ('background-color', '#88d066');
} else {
$('td', tr).addClass("statusHOSTDOWN");
};
};
setTimeout(refresh, 5000);
}
});
JSON example:
[{
"client": "client1",
"ip": "127.0.0.1",
"status": "up"
}, {
"client": "client2",
"ip": "127.0.0.2",
"status": "up"
}]
What would be the best way to rerun the if
statement and update table color with new information from JSON without running the whole function inside setTimeout. Because running the whole function inside seTimeout
just appends more table at the bottom of the existing table without replacing it with new information.