Repeat jQuery ajax call

How to repeat jQuery ajax call every 10 seconds?

$(document).ready(function() {
    $.ajax({    
        type: "GET",    
        url: "newstitle.php",   
        data: "user=success",    
        success: function(msg) {
            $(msg).appendTo("#edix");    
        }  
    });

I've tried to wrap the $.ajax with a function and to call the function with setInterval

$(document).ready(function() {
    function ajaxd() { 
        $.ajax({
            type: "GET",
            url: "newstitle.php",
            data: "user=success",
            success: function(msg) {
                $(msg).appendTo("#edix");
            }
        });
    }
    setInterval("ajaxd()",10000);
});

But it says "ajaxd is not defined"

Answers


Your method should not be placed inside the ready method, or it would only be available there and not outside.

$(document).ready(function() {
    setInterval(ajaxd, 10000);
});

function ajaxd() { 
  $.ajax({
   type: "GET",
   url: "newstitles.php",
   data: "user=success",
   success: function(msg){
     $(msg).appendTo("#edix");
   }
 });
}

Better approach would be to use setTimeout, so you only make a request when the previous one is done.

How it should be done

Imagine that request for some reason (server malfunction, network error) takes longer than defined time. You'll have many simultaneus requests, which isn't any good. And what if you decide to shorten the time difference from 10 seonds to 1 second in the future?

$(function() {
  var storiesInterval = 10 * 1000;
  var fetchNews = function() {
    console.log('Sending AJAX request...');
    $.ajax({
      type: "GET",
      url: "newstitles.php",
      data: {
        user: 'success',
        some: ['other', 'data']
      }
    }).done(function(msg) {
      $(msg).appendTo("#edix");
      console.log('success');
    }).fail(function() {
      console.log('error');
    }).always(function() {
      // Schedule the next request after this one completes,
      // even after error
      console.log('Waiting ' + (storiesInterval / 1000) + ' seconds');
      setTimeout(fetchNews, storiesInterval);
    });
  }
  
  // Fetch news immediately, then every 10 seconds AFTER previous request finishes
  fetchNews();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

$(document).ready(function() {

setInterval(function() { 
  $.ajax({
   type: "GET",
   url: "newstitle.php",
   data: "user=success",
   success: function(msg){
     $(msg).appendTo("#edix");
   }
 });
}, 10000);

});

Need Your Help

gcc error trying to install PIL in a Python2.6 virtualenv

python gcc python-imaging-library virtualenv

I have created a virtualenv with the --no-site-packages option. I get an error trying to install PIL:

How to export all data from table to an insertable sql format?

sql sql-server copy export ssms

I have a Table (call it A_table) in a database (call it A_db) in Microsoft SQL Server Management Studio, and there are 10 rows.