-4

I need to make a call to the server and get batches of 50 records back. I need to do this untill I've had all records. Can somebody help out of this

var stillRecordsAvailable = true;
var start = 1;
   while (stillRecordsAvailable) {
   // get the next batch of records
   $.ajax({
      url: '/getData?start='+start+'&recordLimit=50',
      type: 'GET',
      success: function(data) {
        if(data.records == 0){
           stillRecordsAvailable = false;
        }
        else{
          start += 50;
        // do something
        }    
      },
      error: function(data) {
       // nothing left to do
      stillRecordsAvailable = false;
    }
  });
Sunil
  • 919
  • 15
  • 25
  • 1
    Have you tried anything till now??..Show some efforts first – Pratik Aug 25 '15 at 06:06
  • http://stackoverflow.com/questions/6538470/jquery-deferred-waiting-for-multiple-ajax-requests-to-finish – Shiva Aug 25 '15 at 06:20
  • @Shiva not satisfying my requirerment – Sunil Aug 25 '15 at 06:34
  • @sunil44: Please specify your requirements clearly. – Shiva Aug 25 '15 at 06:43
  • I am accessing records from server in a batch of 50 records per ajax call. but i don't know how much records exists. So i need to break loop after empty response received and also want to increment start variable by 50 in query param to point to next 5o th record – Sunil Aug 25 '15 at 06:47
  • @Shiva I just edited my code according to requirements plz have a look – Sunil Aug 25 '15 at 07:01

1 Answers1

0

Try using recursion, Call same method until you get all records.

// get the next batch of records
var callServer = function() {
  $.ajax({
    url: '/getrecords.json',
    data: {
      data_set_id: dataset.id
    },
    type: 'GET',
    success: function(data) {
      if (data.records != 0) {
        callServer();//Make ajax call again for next records.
      }

      // do something
    },
    error: function(xhr, textStatus, errorThrown) {
      // nothing left to do

    }
  });
}
callServer();
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65