James Pale
James Pale

Reputation: 185

auto refresh a div using javascript after a number of seconds?

i am looking for a way to refresh a div after every 5 seconds without refreshing the entire page.

i have a page called mod_chat.php which i am including in my page profile.php liek so:

<?php include('includes/mod_profile/mod_chat/mod_chat.php'); ?>

in mod_chat.php is the div i want to reload every 5 seconds but without refreshing the entire page, everything i have tried is not working, please can someone show me how to solve this.

here's what ive tried:

<div class="chat_box">
<div class="chat_box_head"><div class="chat_box_head_text">You and  <?php
$chat_set_name = chat_set_name();
while ($chat1 = mysql_fetch_array($chat_set_name)) { ?><?php echo "".$chat1['display_name']."" ?><? } ?></div><div class="chat_close"></div></div>
<div class="chat_content">
<?php
    $chat_set = get_chats_to();
while ($chat = mysql_fetch_array($chat_set)) {
    if (isset($_SESSION['user_id'])) { 
        if ($chat['to_user_id'] == $_SESSION['user_id']){ ?>
             <?php echo "<div class=\"chat_row\">".$chat['content']."<br/><a href=\"#\">{$chat['display_name']}</a></div>"; ?>


        <? } } }?>

       </div>

javascript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
   $('.chat_box').load('mod_chat.php');
}, 05000); // refresh every 05000 milliseconds
</script>

Upvotes: 0

Views: 2820

Answers (1)

Harry Beasant
Harry Beasant

Reputation: 1014

What you could do it this;

function get_chat()   {




$.get('/mod_chat.php', {}, function(data ) {


        $("#div").html(data);
        //optional code, if you wanted the chat box to have a scroller and automatically move to bottom when messages are loaded.
        var textarea = document.getElementById('divid');
            textarea.scrollTop = textarea.scrollHeight;

    });

    //load messages every 4 seconds
    setInterval("get_chat();", 4000);


}

And then in document load, you'd call the function;

 $(document).ready(function(){
        get_chat();
    });

Upvotes: 2

Related Questions