Using Recursions and Closures

  • Note: Use setTimeout to avoid Error Call Stack Size Exceeded
  • JavaScript Code:
cPanel.scheduleMapsSliderStatus = function (rc, func) {
    var runCount = rc;
    var funcName = 'scheduleMapsSliderStatus()';
    var logHeader = myControlPanel.getLogHeaderCF(funcName, func);

    var slideShowStatusSummaryField =  myObjects.getPanel("slideShowStatusSummaryField");
    var bgColor = slideShowStatusSummaryField.css('background-color');
    var fgColor =  slideShowStatusSummaryField.css('color');
    
    function MapsSliderStatusClosure() {
        var mesgFlag = runCount % 2;
        var panelMode = myControlPanel.getPanelMode(funcName);
        var slideShowStatusSummaryField = null;
        if (mesgFlag === 0) {
            updateSliderStatus("Click on PLAY button to start Animation! ");
                // Need to reread slideShowStatusSummaryField Objects as this is recreated by  updateSliderStatus()
            slideShowStatusSummaryField =  myObjects.getPanel("slideShowStatusSummaryField");
            slideShowStatusSummaryField.css('background-color', bgColor);

        } else {
            updateSliderStatus("Click on GREEN Marker to start Slideshow !");
                // Need to reread slideShowStatusSummaryField Objects as this is recreated by  updateSliderStatus()
            slideShowStatusSummaryField =  myObjects.getPanel("slideShowStatusSummaryField");
            slideShowStatusSummaryField.css('background-color', 'green');
        }
        if (runCount === 10 ) {   // run this Animation ONLY 10x
            return;
         }
        if (panelMode === "maps") { // Don't schedule again if panelMode !='maps'
            runCount++;
            setTimeout(MapsSliderStatusClosure, 2000, runCount, funcName)
        }
    };
    MapsSliderStatusClosure();   // Just all the closure function a first time
};

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *