Answer the question
In order to leave comments, you need to log in
Duplicate output in javascript why?
There is an output from JSON (JSON is formed in PHP), there is a timeout. When updating by timeout, the data is duplicated for some reason, and not updated. As a result, the data on the cycle endlessly falls out.
The code:
<ul></ul>
$(document).ready(function(){
$.ajaxSetup ({
cache: false
});
setTimeout(getData, 5000);
});
/* call the php that has the php array which is json_encoded */
function getData(){
$.getJSON('api.php', function(data) {
/* data will hold the php array as a javascript object */
$.each(data, function(key, val) {
$('ul').append('<li id="' + key + '">' + val.date + ' ' + val.event + ' ' + val.region + ' ' + val.host + ' '+ val.type + ' ' + val.info + '</li>');
});
setTimeout(getData, 5000);
});
}
Answer the question
In order to leave comments, you need to log in
In your code, you only add new elements to the list, and therefore the old ones are not deleted. try to insert between $.getJSON('api.php', function(data) { and $.each(data, function(key, val) command to clear the list:
$('ul').empty();
It should look like this:
function getData(){
$.getJSON('api.php', function(data) {
$('ul').empty();
/* data will hold the php array as a javascript object */
$.each(data, function(key, val) {
$('ul').append('<li id="' + key + '">' + val.date + ' ' + val.event + ' ' + val.region + ' ' + val.host + ' '+ val.type + ' ' + val.info + '</li>');
});
setTimeout(getData, 5000);
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question