javascript - 一个函数中的全局变量赋值在另一个函数中无法访问

标签 javascript scope global-variables

我正在使用地理定位来收集坐标并进行 API 调用来获取华氏温度。我在一个函数中为全局变量 tempNum 分配了这个值,该函数使用函数 getWeatherByCooperatives(latitude, longitude) 将温度添加到页面。

稍后,我尝试访问此变量并将该值作为参数传递到另一个函数 typeConversion 中,我试图将温度值转换为摄氏度。它返回 NaN 并且在调试时,我不明白为什么。

这是我的 HTML 和 JS。我觉得自从我在全局级别声明了我的变量并在我为其赋值的函数中设置了“return varName”以来,该值应该可以在我的 JS 中访问;但我可能做错了什么或误解了变量范围。请帮忙。

var place = document.getElementById("meat");
var header = document.getElementById("header");

var weather = document.getElementById("Weather");

var latitude;
var longitude;
var coordinates = document.getElementById("coordinates");


function success(position) {
     latitude = position.coords.latitude;
     longitude = position.coords.longitude;
        getWeatherByCoordinates(latitude, longitude);
    };
    //else {
    //    //Write Code to alternatively show a Zip-Code Search Box;
    //};


navigator.geolocation.getCurrentPosition(success);
var city = document.getElementById("city");
var weatherDescription = document.getElementById("weather-description");
var roundTempF;
var roundTempC;
var tempNum;
var tempStringFFull

function getWeatherByCoordinates(latitude, longitude) {
    
    var fullURL = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=75ed54453a6e806917cfa439b3fb1dd9&units=imperial";

    $.getJSON(fullURL, function (data) {
        
        var tempString = data.main.temp;
        var tempNum = parseInt(tempString);
        roundTempF = Math.floor(tempNum);
        stringF = roundTempF.toString();
        tempStringFFull = stringF + "\xB0" + " F";
        weather.innerText = tempStringFFull;
        city.innerText = data.name;
        weatherDescription.innerText = data.weather[0].description;

        if (data.dt > data.sys.sunrise && data.dt < data.sys.sunset) {
            $("#whole-page").removeClass("whole");
            $("#whole-page").removeClass("night");
            $("#whole-page").addClass("day");
        }
        else {
            $("#whole-page").removeClass("whole");
            $("#whole-page").removeClass("night");
            $("#whole-page").addClass("night");
        };

        event.preventDefault();
    });
    return tempNum;
};



function typeConversion(tempNum) {
    if (changeTempType.innerText === "Celsius") {
        var tempStringC;
        var celsiusDecimal = (tempNum - 32) * (5 / 9);
        roundTempC = Math.floor(celsiusDecimal);
        tempStringC = roundTempC.toString();
        tempStringC += "\xB0" + " C";
        weather.innerText = tempStringC;
        changeTempType.innerText = "Farenheit";
        return;
    }
    else if (changeTempType.innerText === "Farenheit") {
        weather.innerText = tempStringFFull;
        changeTempType.innerText = "Celsius";
        return;
    }
    else {
        weather.innerText = "We are unable to retrieve the weather at this time.  Please try again later";
        changeTempType.innerText = "Celsius";
        return;
    };
};

var changeTempType = document.getElementById("change-temp-type");

changeTempType.addEventListener("click", typeConversion, false);
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
    <title>Weather</title>
</head>
<body id="whole-page" class="whole">
    <div class="wrapper"> 

       
    <h2 id="header">Check the Current Temperaturate by Zip Code</h2>
<label>Farenheit</label>
    <input type="radio" name="temp-type" value="C" id="Celsius-radio"/><label>Celsius</label>-->
        <button id="change-temp-type">Celsius</button>

     <form>    
      <p>Enter the Zip Code to see the Weather there!</p>
      <input id = "Zip-Code" type="text"/>
      
      <input id = "submit-zip" type="button" value="Get Weather!"/>
    </form>
    <div>
            
            <h3 id="city"></h3>
            <h3 id= "Weather" class="temp-text"></h3>
            <h4 id="weather-description"></h4>

      </div>
       
    </div>
<script src= "https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js"> </script> 
   
<script src="javascript/main.js"></script>
</body>
</html>

最佳答案

您有几个问题:

  1. 您在函数中重新声明 var tempNum =,这意味着它将成为一个只能在函数作用域内访问的新变量(保持全局变量不变)
  2. 您在 $.getJSON 中的代码正在使用异步回调 - 这意味着它将比下面的代码晚运行一段时间。当您返回 tempNum 时,该代码尚未运行。
  3. 您的 return 语句实际上并没有做任何事情...只需重新分配全局变量就足够了。

更新 - 我一开始错过的问题:

  • 您正在将 typeConversion 中的函数参数命名为“tempNum”。同样,这将产生一个新变量,只能在该函数的范围内访问。如果您想影响全局变量,那么该函数根本不需要任何参数,tempNum 将按预期引用全局变量。
  • 我已尝试解决以下所有问题。

    var place = document.getElementById("meat");
    var header = document.getElementById("header");
    
    var weather = document.getElementById("Weather");
    
    var latitude;
    var longitude;
    var coordinates = document.getElementById("coordinates");
    
    
    function success(position) {
         latitude = position.coords.latitude;
         longitude = position.coords.longitude;
            getWeatherByCoordinates(latitude, longitude);
        };
        //else {
        //    //Write Code to alternatively show a Zip-Code Search Box;
        //};
    
    
    navigator.geolocation.getCurrentPosition(success);
    var city = document.getElementById("city");
    var weatherDescription = document.getElementById("weather-description");
    var roundTempF;
    var roundTempC;
    var tempNum;
    var tempStringFFull
    
    function getWeatherByCoordinates(latitude, longitude) {
        
        var fullURL = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=75ed54453a6e806917cfa439b3fb1dd9&units=imperial";
    
        $.getJSON(fullURL, function (data) {
            
            var tempString = data.main.temp;
            // removed "var tempNum"...
            tempNum = parseInt(tempString);
            roundTempF = Math.floor(tempNum);
            stringF = roundTempF.toString();
            tempStringFFull = stringF + "\xB0" + " F";
            weather.innerText = tempStringFFull;
            city.innerText = data.name;
            weatherDescription.innerText = data.weather[0].description;
    
            if (data.dt > data.sys.sunrise && data.dt < data.sys.sunset) {
                $("#whole-page").removeClass("whole");
                $("#whole-page").removeClass("night");
                $("#whole-page").addClass("day");
            }
            else {
                $("#whole-page").removeClass("whole");
                $("#whole-page").removeClass("night");
                $("#whole-page").addClass("night");
            };
    
            event.preventDefault();
        });
        //return tempNum;
    };
    
    
    // removed the argument "tempNum", just use the global
    function typeConversion() {
        if (changeTempType.innerText === "Celsius") {
            var tempStringC;
            var celsiusDecimal = (tempNum - 32) * (5 / 9);
            roundTempC = Math.floor(celsiusDecimal);
            tempStringC = roundTempC.toString();
            tempStringC += "\xB0" + " C";
            weather.innerText = tempStringC;
            changeTempType.innerText = "Farenheit";
            return;
        }
        else if (changeTempType.innerText === "Farenheit") {
            weather.innerText = tempStringFFull;
            changeTempType.innerText = "Celsius";
            return;
        }
        else {
            weather.innerText = "We are unable to retrieve the weather at this time.  Please try again later";
            changeTempType.innerText = "Celsius";
            return;
        };
    };
    
    var changeTempType = document.getElementById("change-temp-type");
    
    changeTempType.addEventListener("click", typeConversion, false);
    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet"/>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
        <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
        <title>Weather</title>
    </head>
    <body id="whole-page" class="whole">
        <div class="wrapper"> 
    
           
        <h2 id="header">Check the Current Temperaturate by Zip Code</h2>
    <label>Farenheit</label>
        <input type="radio" name="temp-type" value="C" id="Celsius-radio"/><label>Celsius</label>-->
            <button id="change-temp-type">Celsius</button>
    
         <form>    
          <p>Enter the Zip Code to see the Weather there!</p>
          <input id = "Zip-Code" type="text"/>
          
          <input id = "submit-zip" type="button" value="Get Weather!"/>
        </form>
        <div>
                
                <h3 id="city"></h3>
                <h3 id= "Weather" class="temp-text"></h3>
                <h4 id="weather-description"></h4>
    
          </div>
           
        </div>
    <script src= "https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js"> </script> 
       
    <script src="javascript/main.js"></script>
    </body>
    </html>

    最后一点 - 如果您需要访问 tempNum 的函数稍后运行(例如响应用户操作),这将正常工作。在页面加载后的几毫秒内,它可能仍然是未定义的,因此,如果您尝试在页面加载时正确使用它,它可能仍然是未定义的。在这种情况下,您可能希望将其初始化为某个默认值。

    希望这有帮助。

    关于javascript - 一个函数中的全局变量赋值在另一个函数中无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48589292/

    相关文章:

    javascript - 我的脚本中的全局变量和局部变量

    Javascript 变量为 null 但进入 if 语句

    javascript - 来自网页的多个并发下载

    javascript - 为什么 `Export Default Const`无效?

    javascript - 在函数作用域内声明的变量会导致 if 语句出错

    python - 蓝图中的 Flask 应用程序对象

    php - 使用 PHP 和 MySQL 插入和检索服务器变量

    javascript - 获取点击链接的排名

    php - 如何使用 PHP 从我自己的网站上的 Google Drive 获取文件和文件夹列表?

    C++ 类变量作用域