Reputation: 816
If this was asked, I couldn't find it for the life of me!
Issue:
I have a div that is using a setInterval
to update itself every 1-2 seconds. Basically users are able to submit a question (which is stored in a mysql DB) and the div that is being updated is a queue for admins so they can quickly open a chat session with whoever submits a question.
sosInterval = setInterval(function () {
randomTime = new Date().valueOf();
$("#sosContainer").load("sos.php?action=requestSOS&_=" + randomTime);
}, 1500);
The items that are populated to the div are clickable, so when the admin clicks on the user's name in the queue, it opens a chat session with the user and removes it from the queue. My problem is that sometimes it doesn't do anything when the admin clicks on the name, and I'm thinking it's because they happen to click on it at the exact moment that it's trying to update the div again since it works if they click it again.
Question: So my question is this, when you're using setInterval to update a div with information in nearly real-time, is there any way to prevent the above issue from taking place? I'm open for any opinions, even if it requires having to completely rewrite my system, so be as creative with your opinion as you wish.
Thanks!
Upvotes: 0
Views: 167
Reputation: 113
Append to the queue instead of replacing the entire contents of the div. That way the link won't be removed and replaced every 2 seconds. Also, you should use settimeout instead.
Reason here (https://stackoverflow.com/a/729943/5003581)
Upvotes: 1