0
pushDocs: function (component, event, helper) {
        var pushFiles = component.get('c.pushFilesToEncompass');
        var percComplete = 0;
        var jobStatuses = null;
        pushFiles.setParams({ oppId: component.get('v.recordId') });
        pushFiles.setCallback(this, function (actionResult) {
            component.set("v.filePushJobIds", actionResult.getReturnValue());
            var lastCheck = 0;
            var firstCheck = 0;
            var timeout = 15;
            var elapsed = 0;
            var checkInterval = 2;
            while (percComplete != 100) {
                while ((new Date().getTime() / 1000) - lastCheck < checkInterval) {
                    //do nothing
                }
                var getJobStatuses = component.get('c.getJobStatuses');
                getJobStatuses.setParams({ jobMap: component.get('v.filePushJobIds') });
                getJobStatuses.setCallback(this, function (actionResult) {
                    jobStatuses = actionResult.getReturnValue();
                    var totalJobs = jobStatuses.length;
                    if (totalJobs == 0) {
                        percComplete = 100;
                    } else {
                        var completed = 0;
                        for (var i = 0; i < jobStatuses.length; i++) {
                            var jobStatus = jobStatuses[i];
                            if (jobStatus === 'Completed' || jobStatus === 'Aborted' || jobStatus === 'Failed') {
                                completed += 1;
                            }
                        }
                        percComplete = Math.round((completed * 100) / (totalJobs * 100));
                    }
                });
                $A.enqueueAction(getJobStatuses);
                lastCheck = new Date().getTime() / 1000;
                if (firstCheck == 0) {
                    firstCheck = new Date().getTime() / 1000;
                }
                elapsed = (new Date().getTime() / 1000) - firstCheck;
                if (elapsed > timeout) {
                    break;
                }
            }
            component.set('v.loaded', true);
        });
        $A.enqueueAction(pushFiles);
    }

I want to have a function which will wait on the client side until some server side jobs have been completed but i want a loop to basically give me back the status of these jobs and update a progress bar. The problem i am seeming to have is that my global variable "percComplete" does not update from the callback. I can see that my call back is executed multiple times in this loop, but the my function will always tiem out because the update to percComplete in the callback isn't visible to my loop. ANy ideas?

closed as unclear what you're asking by Pranay Jaiswal, glls, battery.cord, Raul, Reshma May 6 at 9:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1
while ((new Date().getTime() / 1000) - lastCheck < checkInterval)

Do not do this. A spin loop will freeze the UI for the duration (up to a second in this case). Instead, use setInterval or setTimeout to check for progress if you'd like to check periodically.

Because you're using "real" functions, you're creating separate closures; essentially, both functions see different versions of the same variable. Instead, consider using arrow functions, which should allow each function to reference the same variable. Or, if you need IE 11 compatibility, get/set an attribute on the component:

while (component.get("v.percComplete") != 100) {

...

component.set("v.percComplete", 100); // etc...

Not the answer you're looking for? Browse other questions tagged or ask your own question.