javascript - Best way to 'return true' based on conditions from nested functions -
this may novice question trying create function returns true. however, based on happens within several other functions inside.
function checkgeo(){ // check geolocation if( "geolocation" in navigator ) { navigator.geolocation.getcurrentposition( function(position){ sessionstorage.pinlat = position.coords.latitude; sessionstorage.pinlon = position.coords.longitude; // position object set! }); // position not defined if ( position.coords.latitude && position.coords.longitude ){ return true; } } }
this order want things happen geolocation check i'm bit surprised nested if tested before getcurrentposition method finishes.
putting condition within getcurrentposition success function , returning true there not make checkgeo return true. how check if asyncronous function has ended , therefore check results in order return true
?
have function have finished
variable
function checkgeo(){ var self = this; this.ready = function () {} this.result = false; if("geolocation" in navigator) { navigator.geolocation.getcurrentposition(function(position) { sessionstorage.pinlat = position.coords.latitude; sessionstorage.pinlon = position.coords.longitude; self.result = (position.coords.latitude && position.coords.longitude); self.ready.call(self); }); } }
now can use function:
var run = new checkgeo(); run.ready = function () { alert(this.result); //both work alert(run.result); //both work };
a bit more complicated, better programming in opinion.
Comments
Post a Comment