I'm trying to refresh two elements on my page every 4 seconds. My current ajax set up looks like this:
setInterval(function () {
update()
}, 4000);
function update() {
$.get('/hello', function (data) {
console.log("update")
$('#main').html(data);
});
}
Where my HTML has a div with the id main that contains both the sets of s.
After a few seconds of working, my lists start duplicating items like crazy and the page starts lagging. I get an error in my console saying "insufficient resources". In addition, I've noticed that the "update" that is supposed to be logged in the console does not happen every 4 seconds, rather it keeps logging in less than .5 seconds.
I've tried using setTimeout but it doesn't work.
My handlebars code looks like this:
<div id="main">
<div id="recalled">
<ul id="list">
{{#each thing}}
<li>
this.something
</li>
{{/each}}
</ul>
</div>
<div id="more">
<ul>
{{#each anotherThing}}
<li>
this.property
</li>
{{/each}}
</ul>
</div>
</div>
My express code is rendering the entire page after searching (asynchronously) a mongo database.
await User.find( {some search parameter}, function(error, data) {
res.render("thePage", {thing: data[0], anotherThing: data[1})
})
The data that is being rendered initially is fine.
I just want to refresh the list elements every few seconds using ajax to show any updates to a certain property of my User model. The ajax request starts freaking out after a few seconds and is not giving me the output i expect.
Thank you for your help!