javascript - jshint - Don't make functions within a loop - google maps -


i created google map markers (from wordpress posts), when run "gulp" on console have error: "don't make functions within loop."

i created jsfiddle replicate localhost situation, please me fix issue?

i know if write "// jshint ignore:line" gulp create script, think problem problem of bug have on chrome :(

var infowindow = new google.maps.infowindow(); var gmarkers = [];   function initialize() {   map = new google.maps.map(document.getelementbyid('map'), {     zoom: 10,     center: new google.maps.latlng(51.508293, -0.127701),     maptypecontrol: false,     pancontrol: false,     zoomcontroloptions: {         position: google.maps.controlposition.right_center     },     maptypeid: google.maps.maptypeid.roadmap   });    (var = 0; < locations.length; i++) {     var marker = new google.maps.marker({       position: locations[i].latlng,       icon: locations[i].marker,       map: map,       animation: google.maps.animation.drop,       optimized : false     });      gmarkers.push(marker);      google.maps.event.addlistener(marker, 'click',       (function(marker, i) {         return function() {           map.panto(marker.getposition());            //method 2           var target = jquery("#item" + i);           jquery(target).show().siblings("div").hide();            if (!jquery('#all-posts').hasclass('active')){             jquery("#all-posts").toggleclass("active");             jquery("#close-btn").toggleclass("active");           }            // stop yt video           //jquery("#stop-yt-video").trigger("click");         };       })(marker, i)     );     } }  initialize(); 

take iife function, move out of loop , give name, makehandler (but don't invoke it).

function makehandler(marker, i) {   return function() {      map.panto(marker.getposition());      //method 2     var target = jquery("#item" + i);     jquery(target).show().siblings("div").hide();      if (!jquery('#all-posts').hasclass('active')){           jquery("#all-posts").toggleclass("active");           jquery("#close-btn").toggleclass("active");     }     // stop yt video     //jquery("#stop-yt-video").trigger("click");   }; } 

then in loop, this:

for (var = 0; < locations.length; i++) {   var marker = new google.maps.marker({     position: locations[i].latlng,     icon: locations[i].marker,     map: map,     animation: google.maps.animation.drop,     optimized : false   });   gmarkers.push(marker);    google.maps.event.addlistener(marker, 'click', makehandler(marker, i));  } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -