You need to make sure it can be called many times, in which it mustn't append values, but replace (just clear the div at the begining, like document.getElementById("demo").innerHTML = ''). Then, you can use setInterval to call your function every so often. Don't forget to start it only when the page is loaded (you were using jquery for that, there are other options):