<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Loop through layers in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676716#M63063</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You do not want to "sleep".&amp;nbsp; That stops everything including the refresh of the map.&amp;nbsp; You will want to look at timers.&amp;nbsp; Start with this document:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.w3schools.com/js/js_timing.asp" title="http://www.w3schools.com/js/js_timing.asp"&gt;JavaScript Timing Events&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good Luck&lt;/P&gt;&lt;P&gt;Brent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 13 Apr 2015 19:44:07 GMT</pubDate>
    <dc:creator>BrentHoskisson</dc:creator>
    <dc:date>2015-04-13T19:44:07Z</dc:date>
    <item>
      <title>Loop through layers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676715#M63062</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have what seems like a simple request, although it is apparently not.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i have a map div that contains a series of check boxes that represent layer ids.&amp;nbsp; When a box is checked, an array is created and all checked boxes thus layer ids are added to the array.&amp;nbsp; 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.&amp;nbsp; Only when a layer is added, or removed from the array would the loop restart.&amp;nbsp; Below is what I have tried.&amp;nbsp; But it doesn't work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Sleep Function&lt;/P&gt;&lt;P&gt;function sleep(milliseconds) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var start = new Date().getTime();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (var i = 0; i &amp;lt; 1e7; i++) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((new Date().getTime() - start) &amp;gt; milliseconds){&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;//Layer Looper Function&lt;/P&gt;&lt;P&gt;function startLoop(visibleLayerIds) {&lt;/P&gt;&lt;P&gt;for (var t = 0; t &amp;lt; layers.length; t++) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layername = layers&lt;T&gt;.id&lt;/T&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // console.log(layername);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($.inArray(layername, visibleLayerIds) &amp;gt; -1) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log(layername + " set to visible");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layers&lt;T&gt;.show();&lt;/T&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sleep(30000);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layers&lt;T&gt;.hide();&lt;/T&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log(layername + " set to invisible");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;//Update Layer Visibility Function&lt;/P&gt;&lt;P&gt;on(dom.byId("layer0CheckBox"), "change", updateLayerVisibility);&lt;/P&gt;&lt;P&gt;on(dom.byId("layer1CheckBox"), "change", updateLayerVisibility);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;function updateLayerVisibility () {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var inputs = query(".list_item");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var inputCount = inputs.length;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var visibleLayerIds = []&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Get Checked Layers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (var i = 0; i &amp;lt; inputCount; i++) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (inputs&lt;I&gt;.checked) {&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; visibleLayerIds.push(inputs&lt;I&gt;.value);&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;startLoop(visibleLayerIds);&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 16:34:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676715#M63062</guid>
      <dc:creator>CharlesGant</dc:creator>
      <dc:date>2015-04-13T16:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through layers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676716#M63063</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You do not want to "sleep".&amp;nbsp; That stops everything including the refresh of the map.&amp;nbsp; You will want to look at timers.&amp;nbsp; Start with this document:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.w3schools.com/js/js_timing.asp" title="http://www.w3schools.com/js/js_timing.asp"&gt;JavaScript Timing Events&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good Luck&lt;/P&gt;&lt;P&gt;Brent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 19:44:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676716#M63063</guid>
      <dc:creator>BrentHoskisson</dc:creator>
      <dc:date>2015-04-13T19:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through layers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676717#M63064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Brent,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the response.&amp;nbsp; I have worked the setTimeout function into the code, however I think I'm having another issue.&amp;nbsp; It seems that the function containing the two 'timeouts' is continuing through the loop before waiting for the two 'timeouts' to complete.&amp;nbsp; An asynchronous issue perhaps?&amp;nbsp; Still kinda new to JS, but I read that I need to add callbacks.&amp;nbsp; However I'm not sure where to do that in my code snippet.&amp;nbsp; Any advice?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function showLayers(templayer) {&lt;/P&gt;&lt;P&gt;console.log("here");&lt;/P&gt;&lt;P&gt;templayer.show();&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function hideLayers(templayer) {&lt;/P&gt;&lt;P&gt;console.log("here");&lt;/P&gt;&lt;P&gt;templayer.hide();&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Layer Looper Function&lt;/P&gt;&lt;P&gt;function startLoop(visibleLayerIds) {&lt;/P&gt;&lt;P&gt;for (var t = 0; t &amp;lt; layers.length; t++) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; layername = layers&lt;T&gt;.id&lt;/T&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // console.log(layername);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($.inArray(layername, visibleLayerIds) &amp;gt; -1) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log(layername + " set to visible");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; setTimeout(showLayers, 5000,layers&lt;T&gt;);&lt;/T&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; setTimeout(hideLayers, 15000,layers&lt;T&gt;);&lt;/T&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Update Layer Visibility Function&lt;/P&gt;&lt;P&gt;on(dom.byId("layer0CheckBox"), "change", updateLayerVisibility);&lt;/P&gt;&lt;P&gt;on(dom.byId("layer1CheckBox"), "change", updateLayerVisibility);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function updateLayerVisibility () {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var inputs = query(".list_item");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var inputCount = inputs.length;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var visibleLayerIds = []&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Get Checked Layers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (var i = 0; i &amp;lt; inputCount; i++) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (inputs&lt;I&gt;.checked) {&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; visibleLayerIds.push(inputs&lt;I&gt;.value);&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; startLoop(visibleLayerIds);&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Apr 2015 16:19:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676717#M63064</guid>
      <dc:creator>CharlesGant</dc:creator>
      <dc:date>2015-04-14T16:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through layers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676718#M63065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As you have it written, both timeouts for all layers will be run at the same time, as you found out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your startLoop will need to grab only the first layer in the array and start the showLayers timer.&amp;nbsp; Then, your showLayers function will need to start your timer for the hideLayers function.&amp;nbsp;&amp;nbsp; Finally, your hideLayers function will need to increment your layer index, grab the next layer in the array and start the timer for the showLayers function again (or successfully exit if you are beyond the layer array range).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This way you will only have one timer running at a time.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Apr 2015 18:19:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676718#M63065</guid>
      <dc:creator>BrentHoskisson</dc:creator>
      <dc:date>2015-04-14T18:19:11Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through layers</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676719#M63066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Brent,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After following your suggestions, I "think" I got it figured out.&amp;nbsp; Your guidance is much appreciated!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Charles&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Apr 2015 11:55:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/loop-through-layers/m-p/676719#M63066</guid>
      <dc:creator>CharlesGant</dc:creator>
      <dc:date>2015-04-17T11:55:42Z</dc:date>
    </item>
  </channel>
</rss>

