I have a <div id="refresh"> that has some MySQL content in it. I has this JavaScript code for it:
<script type="text/javascript">
var auto_refreshs_contents = setInterval(
function(){
$('#refresh').load('index.php?_=' +Math.random()+' #refresh').fadeIn("slow"); // load div.refresh
}
, 3000); // refresh every 3 seconds
</script>
It actually posts a duplicate of the code the first time so it would look like this:
0 seconds
+-------------------------------------+
|Randome Text |
|This is such a cool chat room |
|Not really |
+-------------------------------------+
3 seconds
+-------------------------------------+
|Randome Text |
|This is such a cool chat room |
|Not really |
|This is such a cool chat room |
|Not really |
+-------------------------------------+
If you noticed that, It left out the post “Random Text”. I don’t know why.
But yeah, it basically does that the first time. Then after 6 seconds, it refreshes, but doesn’t more content, after that, it refreshes right. So I decided to clear the contents of the page right before refreshing it:
<script type="text/javascript">
var auto_clearContents = setInterval(
function(){
$("#refresh").html(""); //clear contents of div#refresh
}
, 3000); //clear existing contents every 3 seconds
var auto_refreshs_contents = setInterval(
function(){
$('#refresh').load('index.php?_=' +Math.random()+' #refresh').fadeIn("slow"); // load div.refresh
}
, 3000); // refresh every 3 seconds
</script>
However thats when things get weird. I’m looking at the networks tab in Chrome Developers Tools and it’s showing that it refreshes every 200ms or so (yes 200) and sometimes it takes 2.5 seconds. I have no idea…
Is there something wrong with my code? Is there a better way to do it? Thanks!
I think that there must be something wrong on the back end. Here is a fiddle showing basically just what you are saying and it’s working fine.
When you see that 200ms, I think that is likely the time that it takes to generate the
index.phppage, not the time between intervals.If you show us the response from the php page or the php page itself we may be able to help.