0

I know this code below will force a page to reload every 5 seconds and display its contents in the assigned div

$(document).ready(function() {
    setInterval(function () {
        $('#comment_display').load('comments.php')
    }, 5000);
});

But I want to know if it's possible to replace the page URL with a div so that it force the div to reload every 5 seconds without reloading the page for example:

<div id="my_div" style="display:none;">
    <!-- run the database query to collect data from database and echo it out -->
</div>
<script>
    $(document).ready(function() {
        setInterval(function () {
            $('#comment_display').load($('#my_div'))
        }, 5000);
    });
</script>
3
  • 1
    use ajax and fetch data from server using databse query and replace your div every five seconds Commented May 9, 2016 at 16:26
  • @Poria... Please I am new to jQuery so I don't know how to do that, please can you give me a clue on how it's been done or paste a link where I can get its details Commented May 9, 2016 at 16:30
  • 1
    blog.teamtreehouse.com/… here is tutorial Commented May 9, 2016 at 16:31

1 Answer 1

1

You can create an external file of PHP (file2.php). In that file you can write query. After every 5 seconds you can send request to that file (file2.php) and than you can replace result in desired div.

Example code setInterval(function(){

$.ajax({
    type: 'POST',
    url: 'file2.php',
    cache: false,
    success:function(data){
        $('#my_div').html(data);
    }

});

}, 5000);

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is a long method of still achieving the same thing I did above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.