title: How to avoid ERR_INSUFFICIENT_RESOURCES on AJAX calls
body: <p>It's been a while since I have tried working with JavaScript, so please bear with me. I am working on an application that offers reports on student data. The backend is PHP.</p>
  
  <p>The database that we use for these reports has to be refreshed from time to time from a central warehouse. The refresh happens at the discretion of the end user and is not automated for various reasons not worth going into here.</p>
  
  <p>The refresh takes at least 10 minutes. So it would be painful for the browser to sit there loading for that amount of time with no feedback going to the user. I figured the best way to offer feedback to the user would be through a simple JQuery script that keeps the user up-to-date on the progress of the update.</p>
  
  <p><strong>Here is the script:</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    var n = 0;
      function increment() {
          n++;
          return n;
      }
      $(document).ready(function() {
          $("#popme").click(function(event) {
              n = 0;
              $.getJSON('/path/to/json_pids', function(data) {
                  var numRecords = data.length;
                  $("#pop-result").html('&lt;p&gt;' + numRecords + ' records&lt;/p&gt;');
                  $.each(data, function(row) {
                      $.ajax({
                          url: '/path/to/json_student_info/' + this,
                          success: function() {
                              increment();
                              $("#pop-result").html('&lt;p&gt;' + n + ' / ' + numRecords + ' records&lt;/p&gt;');
                          }
                      });
  
                  });
              });
          });
      });
  </code></pre>
  
  <p><strong>What's happening in this script:</strong></p>
  
  <p>There's a <code>div</code> with the <code>pop-result</code> ID that gets updated. The <code>/path/to/json_pids</code> returns a JSON array of the matching student IDs.</p>
  
  <p>From there, the script loops through each record and calls <code>/path/to/json_student_info/{student_id}</code>, but does not require anything in return. This second URL calls a script on the backend that creates/updates that student's record in the reporting database.</p>
  
  <p>On success, the script is supposed to increment the number displayed in <code>pop-result</code> so that the user can see the progress toward completion of the script.</p>
  
  <p><strong>The result and my question</strong></p>
  
  <p>The result is a bit of a mess. The JS Console is showing a whole long line of <code>ERR_INSUFFICIENT_RESOURCES</code> errors. The script never makes it all the way through all the records. It might reach ~4,000 out of ~11,000 records and just die from there.</p>
  
  <p>I have a feeling I'm making a rookie mistake here. I've been looking for similar scenarios over the past couple of days but haven't found anything that helps. The best idea I can find is to break the data into chunks, but I still receive the same error and behavior. Is there an alternative/better way to accomplish what I am trying to accomplish, or a way to make this script less intensive on the browser?</p>