Javascript - Getting null error despite using onreadystatechange

前端 未结 1 1319
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 10:28

. Hello y\'all, I\'m trying to implement this tutorial as a module in a page but I am getting a \"weatherData.temperature is null\" error.

I had previously been getting

相关标签:
1条回答
  • 2021-01-27 11:14

    You likely need to check the readyState in your onreadystatechange.

    document.onreadystatechange = function () {
        if (document.readyState === "interactive") {
    
            // ...
    
            getLocationAndWeather();
        }
    };
    

    Sample: document.readyState


    Also you need to change

    <a href="temperature" 
    

    to

    <a id="temperature" 
    

    To get switchUnits running, you'll need

    switchUnits = function switchUnits () {
        if (weatherData.units == "°C"){
            weatherData.temperatureValue = weatherData.temperatureValue * 9/5 + 32;
            weatherData.units = "°F";
        } else {
            weatherData.temperatureValue = (weatherData.temperatureValue - 32) * 5/9;
            weatherData.units = "°C";
        }
    
        weatherData.temperature.innerHTML = weatherData.temperatureValue + weatherData.units + ", ";      
    };
    

    ... so the function is published to the window.

    0 讨论(0)
提交回复
热议问题