I wanna to setup a web messenger. So I want to reload my page every 1 or 2 seconds. Actually I want a silent reload to get new chats from my database. Most methods just reload page and clear all inputs. Please help me.
I tried:
function loadlink(){ $('#links').load('test.php',function () { $(this).unwrap(); }); } loadlink(); setInterval(function(){ loadlink() }, 5000);
And:
<meta http-equiv="refresh" content="2">
2
Answers
Refreshing your whole page every 2 seconds is not an excellent idea.
You should read more on
WebSockets
or useajax
This tag refreshes the whole page, not ideal:
Here I found a complete article about this.
It uses php, MySql (to store chat and authentication) and vanilla js ajax calls to refresh chat.
As others stated executing ajax requests every 2 seconds might slow down your server. Better to try with 3-5 secs.
This example uses 5 seconds.
It is set by setInterval(), using milliseconds. 5000ms = 5 sec.
Here is the ajax part that updates conversations (copied from the example):
Note: it has many more parts as well, the ajax call is only a small slice of the cake.