Jim22150
Jim22150

Reputation: 511

Auto update frontend AJAX script modification with jQuery

I have the script below that works great but want to modify it for a desired effect. Right now it only works on initial page load. But how can I edit it to make it auto-feed every 30 seconds or so?

    $.ajax({
      type: "GET",
      url: "Administration/data/people.xml"
    }).done(function (xml) {
      $(xml).find('fullName').each(function() {
        var fullName = $(this).text();
        $('<button type="button" class="mybutton" name="users" onclick="showUser(this.value)"></button>').attr('value', fullName).html(fullName).appendTo('#loadMe');
      });
    }).fail(function (response, error) {
      $('#info').text('Error!');
    });

EDIT: 1/24/14 3:55 AM

I added the interval function, but now it is on an infinite loop and is not overwriting, but instead adding over and over again.

        $(document).ready(function(){
            setInterval(updateMe,1000);

            function updateMe(){

        $.ajax({
          type: "GET",
          url: "Administration/data/people.xml"
        }).done(function (xml) {
          $(xml).find('fullName').each(function() {
            var fullName = $(this).text();
            $('<button type="button" class="mybutton" name="users" onclick="showUser(this.value)"></button>').attr('value', fullName).html(fullName).appendTo('#loadMe');
          });
        }).fail(function (response, error) {
          $('#info').text('Error!');
        });


            }
        });

Upvotes: 0

Views: 68

Answers (2)

Scary Wombat
Scary Wombat

Reputation: 44814

have a look at javascript setInterval ()

Plenty of examples on the web but the basic syntax is

var intervalID = window.setInterval(code, delay);

Upvotes: 1

Jerodev
Jerodev

Reputation: 33186

Use the setInterval function to call the function every 30000 miliseconds.

Upvotes: 1

Related Questions