My server has a JSON file in it which updates every so often. I would like this ajax call to read that every e.g. 3 seconds and update a table in my html with the new data. The ajax call below works, HOWEVER keeps on appending the same table to the table already present in the html, creating an infinite loop of identical tables one after the other.
I thought my work around it, this:
record = "";
$("#table").append(record);
Would work, but it doesn't. Any thoughts? Thank you. Also, I believe this is not the most efficient way to do it. If you had any thoughts in that regard it would be great.
AJAX CALL
var interval = 3000;
function doAjax() {
$.ajax({
url: "doc/articles.json",
dataType: "json",
type: "get",
cache: false,
success: function(data) {
$(data.articles).each(function(index, value) {
var record = "<tr><td>" + (index+1) + "</td><td>" + value.name+"</td></tr>";
$("#table").append(record);
})
},
complete: function (data) {
setTimeout(doAjax, interval);
record = "";
$("#table").append(record);
}
});
}