0

I have an ajax application, which has code something like this:

$.ajax({
type: "GET",
url: "data.txt",
beforeSend:function(){
},
success:function(response){
 just update responsed data;
}
});

this call is made every second and it just updates the latest data from 'data.txt' which is being updated on server using cron job. Now since it's only function is to update latest data each second so I'll be more interested in the latest ajax call ; so how can I terminate old ajax call that has crossed 4 seconds but yet not completed so that I can reduce the server traffic. And any suggestion if using 'data.html' or 'data.php' instead of 'data.txt' would increase the application performance ? And which web server can perform better than Apache for ajax driven application ? I need these help urgently ; Please do help.

shomrat
  • 3,831
  • 1
  • 16
  • 4
  • There are few use cases where you'd need to make a call every second. Personally, I'd increase the delay to about 5 (depending on the situation). – Bojangles Aug 28 '11 at 22:05
  • You could also take a look at https://github.com/RobertFischer/JQuery-PeriodicalUpdater/ (disclaimer - it's built on work I did quite some time ago.) – John McCollum Aug 28 '11 at 22:18

3 Answers3

0

I don't know how you have it setup at the moment but perhaps it would be better to run your next AJAX call after the latest one completed (or returned an error). So it would be something like:

(function updateData() {
    $.ajax({
        type: 'GET',
        url: 'data.txt',
        beforeSend: function() {
            // Do stuff
        },
        success: function(response) {
            // Handle response
            updateData();
        }
    });
})();

I don't know if there is any performance changes in changing the file type.

Edit: If you do need to just kill the request, you can do so using the technique explained here.

Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0

You could keep track of when your last successful update time was.

function NowMS() {return parseInt(new Date().getTime())}

dataLastUpdateT = -Infinity;

$.ajax({
    type: "GET",
    url: "data.txt",
    success: function(response){
        if (NowMS() > dataLastUpdateT) {
            use(response);
            dataLastUpdateT = NowMS();
        }
    }
}
Miles
  • 1,357
  • 2
  • 12
  • 20
0

You could try this:

function getData(){
    $.ajax({
        type: "GET",
        url: "data.txt",
        timeout:4000,
        beforeSend:function(){

        },
        success:function(response){

        },
        error:function(){

        },
        complete:function() {
            setTimeout(function(){getData();},1000);
        }
    });
}
getData();

this way the ajax request timeouts after 4 seconds and retries each second (regardless of success or timeout)

Also have a look at nginx for example, it is fast and uses less memory than apache to handle client connections

stewe
  • 41,820
  • 13
  • 79
  • 75