I am working on an HTML5 mobile web app built on top of SalesForce. We make heavy use of JavaScript Remoting functions and I would like to know a way to wait for N (multiple) of these remote function calls to complete and then fire another event or function call. The use case we are trying to accomplish is as follows:
- Show jQuery Spinner/Loader with $.mobile.showPageLoadingMsg();
- Call Remote function 1, 2, 3, ..., N.
- Wait for Remote functions 1, 2, 3, ..., N to return but do NOT freeze the browser.
Hide the jQuery Spinner/Loader with $.mobile.hidePageLoadingMsg();
// Show a Loading Spinner while we wait for our remote requests to complete. $.mobile.showPageLoadingMsg(); // Remote Request 1 MyController.f1( function(result, event){ if(event.type == 'exception') { alert('Error: ' + event.message); } else { // Do stuff here such as fill in a form with the results from f1 } }, {escape:true}); // Remote Request 2 MyController.f2( function(result, event){ if(event.type == 'exception') { alert('Error: ' + event.message); } else { // Do stuff here such as fill in a form with the results from f2 } }, {escape:true}); // ... // Remote Request N MyController.fN( function(result, event){ if(event.type == 'exception') { alert('Error: ' + event.message); } else { // Do stuff here such as fill in a form with the results from fN } }, {escape:true}); // I want to wait until all of my requests to finish before I hide the loading spinner. wait_for_all(); $.mobile.hidePageLoadingMsg();