Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  170] [ 5]  / answers: 1 / hits: 19541  / 8 Years ago, mon, may 2, 2016, 12:00:00

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.



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.



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.



Here is the script:



    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('<p>' + numRecords + ' records</p>');
$.each(data, function(row) {
$.ajax({
url: '/path/to/json_student_info/' + this,
success: function() {
increment();
$(#pop-result).html('<p>' + n + ' / ' + numRecords + ' records</p>');
}
});

});
});
});
});


What's happening in this script:



There's a div with the pop-result ID that gets updated. The /path/to/json_pids returns a JSON array of the matching student IDs.



From there, the script loops through each record and calls /path/to/json_student_info/{student_id}, 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.



On success, the script is supposed to increment the number displayed in pop-result so that the user can see the progress toward completion of the script.



The result and my question



The result is a bit of a mess. The JS Console is showing a whole long line of ERR_INSUFFICIENT_RESOURCES 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.



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?


More From » php

 Answers
33

I'm sure the following code still be optimized but here is a throttled approach:



$(document).ready(function() {
// Declare variable to hold data...
var results = [],
length = 0;

$(#popme).click(function(event) {

// Get Data
$.getJSON('/path/to/json_pids', function(data) {

// Store returned results in value accessible by all functions
results = data;
length = results.length;

$(#pop-result).html('<p>' + length + ' records</p>');

processItem();
});
});

function processItem() {
// Check length, if 0 quit loop...
if(results.length) {
// Make a call always referencing results[0] since we're shfiting the array results...
$.ajax({
url: '/path/to/json_student_info/' + results[0],
success: function() {
$(#pop-result).html('<p>' + ((length - results.length) + 1) + ' / ' + length + ' records</p>');
// Remove the first item to prepare for next iteration...
results.shift();
// Yay! for recursive functions...
processItem();
}
});
}
}
});


This should in theory, recursively call your service with the next item after the previous item has finished processing. In other words, it will make the operations seem synchronous as only one at a time will be processed, but it's using callbacks as oppose the the async flag as you mentioned above that that flag is deprecated.


[#62332] Thursday, April 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josuea

Total Points: 609
Total Questions: 121
Total Answers: 104

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
josuea questions
;