$(document).ready(function(){ checkUserLocationDetails(); }); function getUserIPLocation(){ return new Promise(function(resolve, reject){ try { $.ajax({ type: 'GET', url: 'https://login.remotepc.com/rpcnew/api/iplocation', data: {}, dataType:'json', success: function(data) { //var data = JSON.parse(data); if(data.status == "OK" && data.code == 200 ){ var location_data = {}; location_data.country = data.message.country; location_data.country_code = data.message.country_short; location_data.state = data.message.region; location_data.zipcode = data.message.zipcode; location_data.city = data.message.city; location_data.timezone = getTimeZone(); resolve(location_data); } else reject(data); } }); } catch(e){ console.log(e); reject(e); } }); } function setUserLocationDetails(location_data){ sessionStorage.setItem("user_location", JSON.stringify(location_data)); console.log('location data set'); } function getUserLocationDetails(){ return sessionStorage.getItem("user_location"); } function getTimeZone() { //var offset = new Date().getTimezoneOffset(), o = Math.abs(offset); //return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2); return Intl.DateTimeFormat().resolvedOptions().timeZone; } function checkUserLocationDetails(){ return new Promise(function(resolve, reject){ try { var location_data = JSON.parse(getUserLocationDetails()); if(location_data != null ){ if(location_data.timezone !== getTimeZone()){ resolve( getUserIPLocation().then(function(location_data){ setUserLocationDetails(location_data); }, function(error) { console.log(error); } )); }else{ resolve(); } } else{ resolve( getUserIPLocation().then(function(location_data){ setUserLocationDetails(location_data); }, function(error) { console.log(error); } )); } } catch(e){ console.log(e); reject(e); } }); }