Skip to content Skip to sidebar Skip to footer

Read Xml With Jquery/javascript Every 5 Seconds

I have a html index.html: Facebook &

Solution 1:

The following was done in jquery, though you have a slight error with your xml file that lead to parsing issues.

Assuming your xml file is like this:

<?xml version="1.0"?><data><pagetasks="1"messages="3"notifications="3"/></data>

The following code will modify the page accordingly, using jquery of course:

$(document).ready(function() {
    functionget_info() {
        $.ajax({
            type: "GET",
            url: "page.xml",
            dataType: "xml",
            cache: false,
            complete: function(doc) {
                var tasks = $(doc.responseText).find("page").attr("tasks");
                var msgs = $(doc.responseText).find("page").attr("messages");
                var notes = $(doc.responseText).find("page").attr("notifications");
                if (tasks != $('#tasks').text() ||
                    msgs != $('#messages').text() ||
                    notes != $('#notifications').text()) {
                    document.title = "Facebook" + ' NEW NOTIFICATON';
                }
                $('#tasks').text(tasks);
                $('#messages').text(msgs);
                $('#notifications').text(notes);
            }
        });

    }
    setInterval(function() {
        get_info();
    }, 5000);
});

I spent some time developing this, and I know for a fact it works.

Solution 2:

var tid = setTimeout(function_name, 5000);

functionfunction_name() {
  // do some stuff...
  tid = setTimeout(function_name, 5000); // repeat
}
functionabortTimer() { // to stop the timerclearTimeout(tid);
}

Solution 3:

Put your logic inside of a setTimeout method call:

setTimeout(function(){
    alert('I am displayed after 3 seconds!');
}, 3000);

Solution 4:

Trythis:
    (document).ready(function() {
        functiongetPage() {
           var page= $.ajax({
                type: "GET",
                url: "page.xml",
                dataType: "xml",
                async : false,
            }).responseXML;
       $(page).find('page').each(function(){
         var tasks = $(this).attr("tasks");
         var msgs = $(this).attr("messages");
         var notes = $(this).attr("notifications");
         $('#tasks').html(tasks);
         $('#messages').html(msgs);
         $('#notifications').html(notes);
      });
   }
});
    setInterval(getPage,5000);

Post a Comment for "Read Xml With Jquery/javascript Every 5 Seconds"