Struggling with a puzzling issue here. There's a function called updateDate( id ) that updates a specific row in the database using an AJAX-call to a PHP-script. This process takes some time to complete.
Then there's another function named updateDates() which fetches all the IDs that need updating by making an AJAX-call to a different PHP-script. It then loops through these IDs and calls updateDate( id ) for each ID in the loop.
The problem arises when the loop doesn't wait for a response from updateDate( id ) before moving on to the next iteration. This results in multiple simultaneous AJAX-calls (2398572375 to be precise) causing glitches in the system.
Is there a way to make the loop pause until it receives a response from updateDate( id )?
The updateDate( id ) function returns true once the readyState of the AJAX-call is 4, indicating that the response is complete. I have tried adding this condition in the loop:
if( updateDate( id ) ) { Do something. }
But unfortunately, it didn't solve the problem. Any suggestions or insights on how to tackle this issue?
EDIT: Here are snippets of the code as requested.
updateDates():
function updateDates()
{
ids = new XMLHttpRequest();
ids.open( "GET", "<?php echo anchor("index.php/main/ajax/getIdsToUpdate"); ?>", true );
ids.onreadystatechange = function() {
if( ids.readyState == 4 )
{
var response = $.evalJSON( ids.responseText );
if( response.type == 1 )
{
$.each( response.message, function( index, value ) {
if( value != '' )
{
if( updateDate( value ) )
alert('Checked: ' + value);
}
});
}
}
}
ids.send(null);
}
updateDate( id ):
function updateDate( id )
{
req = new XMLHttpRequest();
req.open("GET", "<?php echo anchor("index.php/main/ajax/updateDate/"); ?>" + id, true);
req.onreadystatechange = function() {
if( req.readyState == 4 )
{
var value = $.evalJSON( req.responseText );
// Updating a DIV on the site..
return true;
}
}
req.send(null);
}