Very Basic Javascript Function Call
I have been doing leaflet API since 2 days. I have been stuck in a function call which gives me unexpected behaviour. the code is as follows var it=0; var map = L.map('map1', {
Solution 1:
onDragEnd(event)
executes the function. You just need to pass in the function reference
marker.on('dragend',onDragEnd);
Using () after the function executes the function immediately. You want this method to act as a callback when the drag ends.
And event object is passed in y default, when this method is invoked. So you need not worry about passing it as an argument
function onDragEnd(event) {
To pass in the custom parameter you can try something in these lines..
for(var i=0; i < $('span').length ; i++) {
$('span').eq(i).on('click', bindClick(i));
}
function bindClick(index) {
return function(e) {
dragEnd(e, index);
}
}
function dragEnd(e, index) {
console.log(e);
console.log(index);
}
Code
for (var i = 0; i < $('.marker').length; i++) {
var marker = $('.marker').eq(i);
$('span').eq(i).on('dragend', bindDragEnd(i));
}
function bindDragEnd(index) {
return function (e) {
dragEnd(e, index);
}
}
function dragEnd(e, index) {
console.log(e);
// you can access the marker object
//by accessing it like $('.marker').eq(index)
}
Post a Comment for "Very Basic Javascript Function Call"