in this code this method return undefined despites alert statement print a value ?
function getNearestPoint(idd)
{
var xmlhttp;
result isn't defined at that point, it only gets defined once your callback executes. The order of execution:
getNearestPoint
startsgetNearestPoint
returns undefiend
xmlhttp.onreadystatechange
If you need result from OUTSIDE of this, you should use a callback:
getNearestPoint(idd, cb){
...
xmlhttp.onreadystatechange = function(){
...
cb(result);
}
}
and your calling code changes from:
var result = getNearestPoint(id);
to:
getNearestPoint(id, function(result){
// do something with result;
});