AnsweredAssumed Answered

Loop through layers

Question asked by chagan02 on Apr 13, 2015
Latest reply on Apr 17, 2015 by chagan02

All,

 

I have what seems like a simple request, although it is apparently not.

 

i have a map div that contains a series of check boxes that represent layer ids.  When a box is checked, an array is created and all checked boxes thus layer ids are added to the array.  I now need to loop through the array, and turn on the first layer, wait some amount of time, turn that layer off, move to the next layer in the array and repeat, indefinitely.  Only when a layer is added, or removed from the array would the loop restart.  Below is what I have tried.  But it doesn't work.

 

//Sleep Function

function sleep(milliseconds) {

    var start = new Date().getTime();

    for (var i = 0; i < 1e7; i++) {

    if ((new Date().getTime() - start) > milliseconds){

    break;

        }

    }

}

          

//Layer Looper Function

function startLoop(visibleLayerIds) {

for (var t = 0; t < layers.length; t++) {

    layername = layers[t].id

    // console.log(layername);

    if ($.inArray(layername, visibleLayerIds) > -1) {

    console.log(layername + " set to visible");

    layers[t].show();

    sleep(30000);

    layers[t].hide();

    console.log(layername + " set to invisible");

        }

    }

}

          

//Update Layer Visibility Function

on(dom.byId("layer0CheckBox"), "change", updateLayerVisibility);

on(dom.byId("layer1CheckBox"), "change", updateLayerVisibility);

          

function updateLayerVisibility () {

    var inputs = query(".list_item");

    var inputCount = inputs.length;

    var visibleLayerIds = []

    //Get Checked Layers

    for (var i = 0; i < inputCount; i++) {

    if (inputs[i].checked) {

    visibleLayerIds.push(inputs[i].value);

    }

}

startLoop(visibleLayerIds);

}

Outcomes