Update image with timer using AJAX

Asked

Viewed 667 times

1

I have this function:

 if ($files_count == 0) {
    ?>
    <img src="img/upload.gif">
    <?php
 }

And I needed this to update every five seconds refresh on the page. I have tested several AJAX functions and I can’t. How to solve this?

  • Hmmm... much information is missing here to be able to answer this question... What is the $files_count?, can explain better what you want to do?

  • Hello, I already solved the problem. files_count is a variable with information from a mysql table. I will post the solution

1 answer

1

Hiccup!

HTML:

<div class="container"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">

SCRIPT:

setInterval(function () {
    check_files();
}, 5000);

function check_files() {
    $.ajax({
        url: 'path/to/php/file.php',
        type: 'GET',
        cache: false,
        data: {},
        success: function (resp) {
            if (resp == 0) {
                $('.container').html('<img src="img/upload.gif" />');
            } else {
                /// mais alguma coisa
            }
        }
    });
}
  • Can you explain how this code solved your problem? So it is useful for others who may have the same problem in the future

  • 1

    And by the way, I think sending AJAX orders with timer can be a problem. It was best to make a new request with setTimeout shot from within the success or at least there is a flag there that the setibterval vefiric.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.