In my code, I have combined 3 separate ajax calls in one function along with checkAjaxCompletion
method to monitor each ajax completion flag.
The current approach involves sending multiple independent ajax calls and using an interval method to continuously check the completion flags. However, I am considering exploring alternative methods instead of relying on intervals.
This is how the code looks like:
function manyAjax() {
setInterval( function() { checkAjaxCompletion(); } , 200);
ajax1();
ajax2();
ajax3();
}
function ajax1() {
//send ajax request to server and update flag based on success or error
}
function ajax2() {
//send ajax request to server and update flag based on success or error
}
function ajax3() {
//send ajax request to server and update flag based on success or error
}
function checkAjaxCompletion() {
if(ajax1_flag == 1 && ajax2_flag == 1 && ajax3_flag == 1) {
//all ajax requests completed successfully
}
else if(ajax1_flag == 2 || ajax2_flag == 2 || ajax3_flag == 2) {
//some ajax requests failed
}
else {
//not all ajax requests are completed yet
}
}
I am concerned about the inefficiency of using the interval function repeatedly and believe there might be a more optimal solution. I am contemplating the use of the observer pattern
, but I am open to suggestions and insights from others.