Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Preventing code under location variable from running until it is assigned

I have this code where I am essentially displaying a notice and upon clicking accept I am grabbing the geolocation of the user and then passing it to a function to get the proper cctld that the user should be redirected to, and assigning it to temp in the ccNoticeAccept.onclick function. The problem is I don’t want anything else to run until I’ve grabbed the cctld and assigned some value to temp. Right now it will essentially run anything under the assignment regardless of whether the user accepts the browsers notification to grant access to location. How can I fix this so nothing under temp runs until a value is assigned? I’ve tried a couple methods but it didn’t seem to work.

//RETURNING ccTLD CODE
function getcctld(code){
  switch (code) {
    case 'us':
      console.log('US detected');
      return "com";
      break;
    case 'ca':
      console.log('CA detected');
      return 'ca';
      break;
    default:
      console.log('No site detected for country');
      return "non";
  }
}

function getlocation(){
  if(navigator.geolocation){
    console.log("geolocation connected");
    navigator.geolocation.getCurrentPosition(function(position){
      $.getJSON('https://nominatim.openstreetmap.org/reverse',{
        lat: position.coords.latitude,
        lon: position.coords.longitude,
        format: 'json'
      }, function(result){
           curcountry = getcctld(result.address.country_code);
           console.log("." + curcountry + " returned");
           return curcountry;
         });
    });
  } else {
     console.log("geolocation not connected");
     return "FAILED";
  }
}

ccNoticeAccept.onclick = function(){
  //if cookie notice accepted, set cookie and display country modal
  setCookie("cookieNoticeCC", 5, "TRUE");
  ccNotice.style.display = "none";

  let temp = getlocation();
  //don't do anything until country is returned

  //more code
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

getlocation isn’t returning anything. Anyway, you can solve this by passing a callback as a parameter to getlocation. Inside getLocation the callback function can be invoked with the country.

function getlocation(callback) {
    if (navigator.geolocation) {
        console.log("geolocation connected");
        navigator.geolocation.getCurrentPosition(function (position) {
            $.getJSON('https://nominatim.openstreetmap.org/reverse', {
                lat: position.coords.latitude,
                lon: position.coords.longitude,
                format: 'json'
            }, function (result) {
                curcountry = getcctld(result.address.country_code);
                console.log("." + curcountry + " returned");
                callback(curcountry);
            });
        });
    } else {
        console.log("geolocation not connected");
        return "FAILED";
    }
}


ccNoticeAccept.onclick = function(){
    //if cookie notice accepted, set cookie and display country modal
    setCookie("cookieNoticeCC", 5, "TRUE");
    ccNotice.style.display = "none";
  
    getlocation((country) => {
        // everything that depends on `country` can be added here
        console.log(country)
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading