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!