0

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!

Fata1Err0r
  • 836
  • 1
  • 6
  • 14
  • 1
    Yes, reduce the interval to 30 seconds and implement long-polling. or, even better, switch to websockets. Another option would be to not replace the whole div, and instead just modify it with any new information. – Kevin B Jun 12 '15 at 17:47
  • Unfortunately, the interval can't be set to that high. Admins have to be informed immediately, and users addressed immediately. We lose a lot of money for every 8 seconds that their issue isn't resolved. – Fata1Err0r Jun 12 '15 at 17:48
  • Please check the sos.php?action=requestSOS response time – Mitul Jun 12 '15 at 17:48
  • 1
    You can still get an instant notification with a 30 second interval, if you use long-polling. :) – Kevin B Jun 12 '15 at 17:48
  • Okay, thanks Kevin. I'll check into that for certain. Great idea. – Fata1Err0r Jun 12 '15 at 17:49

1 Answers1

1

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)

Community
  • 1
  • 1