JavaScript function that I want to call from my ASP.NET Core Blazor app:
export function onClick(map) {
var latLng = null;
function onMapClick(e) {
latLng = map.unproject(e.point).toString();
alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng"
}
map.on('click', onMapClick);
alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable
return String(yourGlobalVariable);
}
C# code where I'm calling that function:
string res = await mapModule.InvokeAsync<string>("onClick", mapInstance);
Where mapInstance and mapModule are IJSObjectReference
The question is: Why in the first alert I'm getting an actual value that I've assigned to latLng variable in the function onMapClick and then I get null outside the function although the variable is global?
I've tried similar code here: http://jsfiddle.net/mufjkv6r/1/ and it works
The question is: Why in the first alert I'm getting an actual value that I've assigned to latLng variable in the function onMapClick and then I get null outside the function although the variable is global?
First thing I want to say is Jsfiddle contain normal flow of execution, which is executing one line after another.
var latLng = null; function onMapClick(e) { latLng = map.unproject(e.point).toString(); alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng" } map.on('click', onMapClick); alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable return String(yourGlobalVariable);
but the above code will not call onMapClick function unless you clicked on the map. So the latLng will be null at that point of time.