admin管理员组文章数量:1182776
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 create google maps marker
marker = new google.maps.Marker({
position: new google.maps.LatLng(result.lat, result.lng),
map: map,
id: result.id
});
google.maps.event.addListener(marker, 'click', function() {
createWindow(marker.id); //<==== this doesn't work because marker always points to the last results when this function is called
});
}
How to solve this?
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 create google maps marker
marker = new google.maps.Marker({
position: new google.maps.LatLng(result.lat, result.lng),
map: map,
id: result.id
});
google.maps.event.addListener(marker, 'click', function() {
createWindow(marker.id); //<==== this doesn't work because marker always points to the last results when this function is called
});
}
How to solve this?
Share Improve this question edited Jul 10, 2019 at 14:47 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Nov 2, 2009 at 19:54 DerkDerk 1812 gold badges2 silver badges6 bronze badges4 Answers
Reset to default 24Try this:
with ({ mark: marker }) {
google.maps.event.addListener(mark, 'click', function() {
createWindow(mark.id);
});
}
An example that demonstrates the use of with
:
for (var i = 0; i < 10; i++) {
setTimeout(function() { console.log(i); }, 1000);
}
The above will log 10
ten times.
for (var i = 0; i < 10; i++) {
with ({ foo: i }) {
setTimeout(function() { console.log(foo); }, 1000);
}
}
This will log 0
to 9
, as desired, thanks to with
introducing a new scope.
JavaScript 1.7 has a let
statement that is nicer, but until that is widely supported, you can use with
.
And use var
for your variables.
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));
Looks like you've got a closure problem. See these questions:
- google maps api all markers opening the same infowindow
- google maps trouble closures passing by reference
- dynamically adding listeners to google maps markers
try this one
var marker = new Array();
for (var i = 0; i < data.results.length; i++) {
result = data.results[i];
// do stuff and create google maps marker
marker[i] = new google.maps.Marker({
position: new google.maps.LatLng(result.lat, result.lng),
map: map,
id: result.id
});
google.maps.event.addListener(marker[i], 'click', example(marker[i].id));
}
create new function
function example(my_window){
return function(){
createWindow(my_window);
}
}
本文标签: javascriptPass parameter to callback functionStack Overflow
版权声明:本文标题:javascript - Pass parameter to callback function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738220367a2069981.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论