Making async calls synchronously in JavaScript using callback.
//This is an async method;
//We wanted geo location before making ajax call.
function geolocation(callback) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude.toFixed(3)
var longitude = position.coords.longitude.toFixed(3)
var location='{"lat" : '+latitude+',"long" : '+longitude+'}'
callback(JSON.parse(location))
})
}
//get geo location using callback & then make POST request
geolocation(function(geo) {
$.ajax({
url: ANALYTICS_HOST + '/store_session',
data: JSON.stringify(geo),
contentType: 'application/json',
dataType: 'json',
type: 'POST'
})
});
Comments
Post a Comment