Richard Cheng Richard Cheng - 7 months ago 15
PHP Question

how to change multiple div every 5 seconds

I tried to update my posts every 5 seconds, like the time is updated, how many users commented and how many likes.
this is the code i tried so far


Some text1


<div id='poster' class='1'>
<input type='hidden' value='1' id='post_id'>
Some text1
</div>

<div id='poster' class='2'>
<input type='hidden' value='2' id='post_id'>
Some text2
</div>

<div id='poster' class='3'>
<input type='hidden' value='3' id='post_id'>
Some text3
</div>

<div id='poster' class='4'>
<input type='hidden' value='4' id='post_id'>
Some text4
</div>


in the body there is onload='refresh()'
here is what's in my java.js:

function refresh(){
var post_id = $("#post_id").val();
setTimeout(refresh_post(post_id),5000);
}

function refresh_post(post_id){
$("."+post_id+"").load("post.php?post_id="+post_id);
}


but it only updates the first div the rest remains the same

Answer

Use setinterval if you want to update post at every 5 second continuosly.

setInterval(function(){
 $('input:hidden').each(function() {
 var post_id = $(this).attr('id');
 refresh_post(post_id)
});
}, 5000);
Comments