How to Make a jQuery AJAX Call Every 10 Seconds?

Patricia Arquette
Release: 2024-11-04 14:52:02
Original
531 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!