Home >Web Front-end >JS Tutorial >Auto Refresh Div Content Using jQuery and AJAX

Auto Refresh Div Content Using jQuery and AJAX

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2025-03-08 00:58:10923browse

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optionally displayed during the AJAX request.

Key Concepts:

  • AJAX's .load() method: Provides a concise way to fetch and update a div's content.
  • setInterval(): Schedules a recurring function call to refresh the data.
  • $.ajaxSetup() (optional): Configures AJAX settings globally, including handling a loading indicator.

Demo:

The demo showcases a live update of blog posts from an RSS feed, including a timestamp indicating the last data refresh. A loading GIF is shown while fetching new data. [View Demo](Not Provided - The provided text mentions a demo but doesn't include a link or image)

Auto Refresh Div Content Using jQuery and AJAX

jQuery and AJAX Code:

This code uses $.ajaxSetup() to manage a loading image during the AJAX request (optional) and then uses .load() to initially populate and subsequently refresh the #content div every 9 seconds (adjust as needed).

(function($) {
    $(document).ready(function() {
        $.ajaxSetup({
            cache: false,
            beforeSend: function() {
                $('#content').hide();
                $('#loading').show();
            },
            complete: function() {
                $('#loading').hide();
                $('#content').show();
            },
            success: function() {
                $('#loading').hide();
                $('#content').show();
            }
        });
        var $container = $("#content");
        $container.load("rss-feed-data.php");
        var refreshId = setInterval(function() {
            $container.load('rss-feed-data.php');
        }, 9000); // Refresh every 9 seconds
    });
})(jQuery);

Auto Refresh Div Content Using jQuery and AJAX

PHP Data Script (rss-feed-data.php):

This PHP script retrieves data from an RSS feed (http://blogoola.com/blog/feed/), formats it into an HTML <code><ul></ul> list, and appends the current date and time.

<?php
$feed_url = 'http://blogoola.com/blog/feed/';
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
$feedData = '';
$date = date("Y-m-d H:i:s");

$feedData .= "<ul>";
foreach ($x->channel->item as $entry) {
    $feedData .= "
  • " . $entry->title . "
  • "; } $feedData .= ""; $feedData .= "

    Data current as at: " . $date . "

    "; echo $feedData; ?>

    Complete HTML Structure:

    This includes the necessary divs and the loading image.

    <title>Auto Refresh Div Content Demo | jQuery4u</title>
    
    
    <div id="wrapper">
        <div id="content"></div>
        <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174136669518374.gif" class="lazy" alt="Auto Refresh Div Content Using jQuery and AJAX ">
    </div>

    Frequently Asked Questions (FAQs): (The original FAQs section is retained and slightly reformatted for clarity.) This section answers common questions about auto-refreshing divs using jQuery and AJAX. (The answers are omitted for brevity, but the original content provides comprehensive answers).

    This revised output maintains the original information while improving readability and structure. Remember to replace /uploads/20250301/174078898967c254fd37454.webp and /uploads/20250301/174078898967c254fd63994.webp with actual image URLs if you want to include the images.

    The above is the detailed content of Auto Refresh Div Content Using jQuery and AJAX. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn