Skip to content Skip to sidebar Skip to footer

Determine When A Boolean Changes From True To False

I've got a webpage that has a print button. As soon as the print button is pressed I have a function function pWindowNeeded() { if (newPWindowNeeded == 'Y') { return t

Solution 1:

The least efficient and easiest way to do it is to poll for the value using setTimeout.

functioncallbackWhenWindowNotNeeded(cb) {
    if (!pWindowNeeded()) {
        cb(); 
    } else {
        // The lower the number, the faster the feedback, but the more// you hog the systemsetTimeout(callbackWhenWindowNotNeeded, 100);
    }
}

functionalertWindow() {
    var w = window.open('','',' width = 200, height = 200, top = 250 , left = 500 ');
    w.document.write("Please Wait<br> Creating Document(s).<br><img src='loadingimage.gif'>");
    w.focus();

    callBackWhenWindowNotNeeded(function() {
        w.close();
    });
}

Ideally you'd use some sort of MessageBus to prevent polling. Here's an example with a poor man's bus.

varMessageBus = (function(){
   var listeners = [];
   return {
    subscribe: function(cb) {
        listeners.push(cb);
    },
    fire: function(message) {
        listeners.forEach(function(listener){
            listener.call(window);
        });
    }
})();

functionalertWindow() {
    var w = window.open('','',' width = 200, height = 200, top = 250 , left = 500 ');
    w.document.write("Please Wait<br> Creating Document(s).<br><img src='loadingimage.gif'>");
    w.focus();

    MessageBus.subscribe(function(message, event) {
        if (message == 'WINDOW_NOT_NEEDED') {
            w.close();
        }
    });
}

// Then wherever you set your newPWindowNeeded
newPWindowNeeded = 'N';
MessageBus.fire('WINDOW_NOT_NEEDED');

Post a Comment for "Determine When A Boolean Changes From True To False"