0

I'm writing javascript code to update the interface from the user. This task has to be done in the background and shouldn't block anything. The code is currently blocking/crashing the browser, the while loop is causing the issue.

What i would like is to wait till the checking and installing are complete and then perform other actions also in between. I want to avoid to write the scenario: TimeOut in a TimeOut in a TimeOut, which does work but it makes the code a mess.

updateChecking();

function updateChecking() {
    setTimeout(function() {

        if (settings.IsChecking === "True") {

            var isChecking = false;
            var isInstalling = false;

            // PROBLEM, Wait till checking is completed
            while (isChecking) {
                var timerUpdateChecker = setInterval(function() {
                    getIsCheckingUpdates().done(function(resultIsChecking) {

                        if (resultIsChecking === "False") {
                            isChecking = true;
                            clearInterval(timerUpdateChecker);
                        }
                    });
                }, 1000);

                checkForSystemUpdates();

                startCheckingDownloads();

                // PROBLEM, Wait till installing is completed
                while (isInstalling) {
                    setInstallerInterface();

                    var timerInstallChecker = setInterval(function() {

                        getIsInstallingUpdates().done(function(resultIsUpdating) {

                            if (resultIsUpdating === "False") {
                                isInstalling = true;
                                clearInterval(timerInstallChecker);
                            }
                        });
                    }, 1000);
                }

                viewUpdateInstalledAlert();

                getAvailableUpdates();

                unsetInstallerInterface();
            };
        }
    }, 0);
}

Any suggestions that could solve my issue?

1
  • Rerunning the checker every second using setInterval might not be a good idea. Commented Jun 17, 2015 at 16:51

1 Answer 1

2

While loops run until the condition if false. Now you're setting the variable to false and call while. So I'm not sure how the while will work. From what I understand it should be while(!isChecking).

That said, you should maybe try to replace your setTimeout and setInterval and replace with events. You can create custom event, dispatch and listen. I think it would be much more efficient. Something like this:

var event = new Event('isInstalled');

In your installed function you dispatch the event:

window.dispatchEvent(event);

And you listen to the event to trigger wanted functions.

window.addEventListener('isInstalled', function(){ 
    //whatever needs to happen when installed is done
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.