简体   繁体   中英

Javascript - form submit and refresh only once

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

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

Look this answer and this article , for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM