Pass parameter to callback function

前端 未结 4 1858
有刺的猬
有刺的猬 2021-02-20 16:35

My code

// do ajax request and get JSON response

for (var i = 0; i < data.results.length; i++) {  
    result = data.results[i];
    // do stuff and c         


        
4条回答
  •  野的像风
    2021-02-20 16:42

    The classic closure problem strikes again!

      google.maps.event.addListener(marker, 'click', function(id) {
        return function(){
          createWindow(id); //<==== this doesn't work because marker always points to the last results when this function is called
        }
      }(marker.id));     
    

提交回复
热议问题