0

Currently I have a script that refreshes my 'chat' page every 10 seconds, as it retrieves the chat from a database. However at the moment once the page is loaded I have to wait 10 seconds (or the seconds i specify in the current js) for the chat to show.

I am not sure how to go about making the chat load initially on window.onload just once, and then every 10seconds. I've tried quite a few ways but they just lead to the onload constantly executing.

current js

<script>
        window.onload = function() {
        window.setTimeout('document.chatMsgListForm.submit()', 10000)
    }
</script>

I gave this a shot too

<script>
var done;
window.onload = function(){
    while(!done) {
      document.forms['formChat'].submit()
      done=true;
    }
}

window.onload = function() {
    window.setTimeout('document.formChat.submit()', 10000)
}
</script>

But no luck unfortunately.

Thank you

0xdw
  • 3,755
  • 2
  • 25
  • 40
  • 2
    That is because you are submitting the form, which I'm guessing is on a single HTML page. You need to look at using AJAX or something similar. This way you send the data and get back the response without refreshing the HTML. You then need to update the HTML with the response. Google search for "AJAX chat example" returns heaps of examples. – Tigger Apr 23 '16 at 02:13
  • Thank you, I'll look into this. – user3196556 Apr 23 '16 at 02:14

1 Answers1

0

You should to look at using AJAX polling or long polling with WebSockets or something else.

Look this answer and this article, for example.

Community
  • 1
  • 1
Horo
  • 82
  • 1
  • 10