im getting value from jquery ajax call but unable to save in global variable, which value i want to use in another function. i want to get assign dustValue in initialize function.
javascript code
var dustValue;
$.ajax({
type: "GET",
url: "http://my url",
dataType: "json",
success: function (data) {
dustValue = data.sensorsdata.PHValue;
}
});
var myCenter = new google.maps.LatLng(22.8046, 86.2029);
function initialize() {
var mapProp = {
center: myCenter,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var value = dustValue;//78.55
var dustbinstatus = '';
if (value < 50) {
dustbinstatus = 'img/dustbinempty.png';
} else if (value > 50 && value < 90) {
dustbinstatus = 'img/dustbinfull.png';
} else if (value > 90) {
dustbinstatus = '3.jpg';
}
var marker = new google.maps.Marker({
position: myCenter,
icon: v_icon
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
body
<div id="googleMap" style="width:1580px;height:780px;"></div>
ajax call is asynch so you can do like below. I mean move your google.maps.event line inside success function
var dustValue;
$.ajax({
type: "GET",
url: "http://my url",
dataType: "json",
success: function (data) {
dustValue = data.sensorsdata.PHValue;
google.maps.event.addDomListener(window, 'load', initialize);
}
});
var myCenter = new google.maps.LatLng(22.8046, 86.2029);
function initialize() {
var mapProp = {
center: myCenter,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var value = dustValue;//78.55
var dustbinstatus = '';
if (value < 50) {
dustbinstatus = 'img/dustbinempty.png';
} else if (value > 50 && value < 90) {
dustbinstatus = 'img/dustbinfull.png';
} else if (value > 90) {
dustbinstatus = '3.jpg';
}
var marker = new google.maps.Marker({
position: myCenter,
icon: v_icon
});
marker.setMap(map);
}
or you can use when function
$.when(ajaxfunction()).done(your logic);