Home  >  Article  >  Backend Development  >  How to Make a jQuery AJAX Call Every 10 Seconds?

How to Make a jQuery AJAX Call Every 10 Seconds?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-04 14:52:02576browse

How to Make a jQuery AJAX Call Every 10 Seconds?

Call jQuery AJAX Every 10 Seconds

In your code, you're using a delay after setting the feedback and then calling get_fb() again when the delay is over. However, this only runs twice and stops because the delay() function doesn't repeat the running of the function. To have your AJAX call run every 10 seconds, you have a few options:

1. setInterval():

Use setInterval() to set a timer that will repeatedly call get_fb() after every 10 seconds. For example:

<code class="js">setInterval(get_fb, 10000);</code>

2. jQuery success() or complete() callback:

You can also use the success() or complete() callback of your AJAX request to set up the next call. For example:

<code class="js">function get_fb(){
    var feedback = $.ajax({
        type: "POST",
        url: "feedback.php",
        async: false
    }).success(function(){ // or use .complete() here
        setTimeout(function(){get_fb();}, 10000);
    }).responseText;

    $('div.feedback-box').html(feedback);
}</code>

This will run the get_fb() function every 10 seconds after the AJAX call has completed.

Note: Ensure that your PHP script correctly generates new feedback values each time you call it.

The above is the detailed content of How to Make a jQuery AJAX Call Every 10 Seconds?. 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