Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用切换来运行JS函数,该函数需要来自GET调用的json_Javascript_Jquery_Html_Json - Fatal编程技术网

Javascript 使用切换来运行JS函数,该函数需要来自GET调用的json

Javascript 使用切换来运行JS函数,该函数需要来自GET调用的json,javascript,jquery,html,json,Javascript,Jquery,Html,Json,我正在努力使用切换将输入的开尔文温度转换为C,然后再转换为F。目前它可以加载页面,因为它默认为摄氏度,但当我切换locationLook之外的功能时,它会运行,无法访问GET中提供的温度。有没有人能提供一些线索或资源来帮助我解决我的难题?我正在尝试学习JS,作为一个编程新手。据我所知,这是一个范围问题,外部函数在初始运行后无法访问json 这是我的HTML切换按钮 华氏度 这是我的js函数,用于根据切换从C切换到F: function factorClick(rawTemp) {

我正在努力使用切换将输入的开尔文温度转换为C,然后再转换为F。目前它可以加载页面,因为它默认为摄氏度,但当我切换locationLook之外的功能时,它会运行,无法访问GET中提供的温度。有没有人能提供一些线索或资源来帮助我解决我的难题?我正在尝试学习JS,作为一个编程新手。据我所知,这是一个范围问题,外部函数在初始运行后无法访问json

这是我的HTML切换按钮

华氏度 这是我的js函数,用于根据切换从C切换到F:

    function factorClick(rawTemp) {
      if (document.getElementById("myCheck").checked == true) {
       //setup Celsius
       var temp = Math.round(rawTemp - 273.15);
       document.getElementById("temperature").innerHTML = temp + " °C";
       return rawTemp;
    } else {
     //setup farenheit
     var temp = Math.round((rawTemp - 273.15 * 1.8) + 32);
     document.getElementById("temperature").innerHTML = temp + " °F";
     return rawTemp;
    }
此函数从weather API获取json,然后更新HTML:

    function locationLook(latitude, longitude) {
  var json = $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=000000000000000000000000000", function(json) {
    console.log(json)
    var ktemp = json.main.temp
    factorClick(ktemp)
    icons(json)
    document.getElementById("description").innerHTML = json.weather[0].main;
    document.getElementById("location").innerHTML = json.name + ", " + json.sys.country;

  });
}

首先,应将温度存储在功能之外。当您的应用程序启动getWeather()时,会调用该函数,在它从服务器获取数据后,您可以在天气对象中设置温度

var weather = {};

function getWeather(position) {
     weather.temperature = 40; // data from api.openweathermap.org 
}

getWeather(myPosition)
然后使函数在点击切换时被调用。你可以从你的物体上获取温度->(天气,温度)。大概是这样的:

toggle.addEventListener('click', function () {
    if (document.getElementById("myCheck").checked == true) {
        document.getElementById("temerature").innerHTML = Math.round(weather.temperature - 273.15) + " °C";

    } else {
        document.getElementById("temerature").innerHTML = Math.round((wether.temerature - 273.15 * 1.8) + 32) + " °F";
    }
}, false)
问题

最初,您将
ktemp
作为参数传递给
factorClick
-

var ktemp = json.main.temp
factorClick(ktemp)
单击时,
factorClick
未传递
ktemp
参数-

<input onclick='factorClick()' id="myCheck" type="checkbox" checked="true">F°<span class="toggle"
由于
未定义的操作返回
NaN
temp
不是一个数字

解决方案

你是对的,这是任何范围的问题。变量
ktemp
当前在本地作用于
locationLook
函数。通过在全局范围内定义
ktemp
,可以在
factorClick
范围内引用
ktemp

比如说-

var ktemp;

//Location functions (gets run in geoSuccess)
function locationLook(latitude, longitude) {
  var json = $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=0426deb1eeda2f667e8c4f40675474fe", function(json) {
    console.log(json)
    ktemp = json.main.temp
    factorClick()
    icons(json)
    document.getElementById("description").innerHTML = json.weather[0].main;
    document.getElementById("location").innerHTML = json.name + ", " + json.sys.country;

  });
}

...

function factorClick() {

  if (document.getElementById("myCheck").checked == true) {

    // Use global ktemp variable
    var temp = Math.round(ktemp - 273.15);

    document.getElementById("temperature").innerHTML = temp + " °C";
    return ktemp;
  } else {
    //setup farenheit
    var temp = Math.round((ktemp - 273.15 * 1.8) + 32);
    document.getElementById("temperature").innerHTML = temp + " °F";
    return ktemp;
  }
}

有关更多信息,请参阅。

这是切换按钮F°C°花了我一段时间才恢复,但非常感谢!
var ktemp;

//Location functions (gets run in geoSuccess)
function locationLook(latitude, longitude) {
  var json = $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=0426deb1eeda2f667e8c4f40675474fe", function(json) {
    console.log(json)
    ktemp = json.main.temp
    factorClick()
    icons(json)
    document.getElementById("description").innerHTML = json.weather[0].main;
    document.getElementById("location").innerHTML = json.name + ", " + json.sys.country;

  });
}

...

function factorClick() {

  if (document.getElementById("myCheck").checked == true) {

    // Use global ktemp variable
    var temp = Math.round(ktemp - 273.15);

    document.getElementById("temperature").innerHTML = temp + " °C";
    return ktemp;
  } else {
    //setup farenheit
    var temp = Math.round((ktemp - 273.15 * 1.8) + 32);
    document.getElementById("temperature").innerHTML = temp + " °F";
    return ktemp;
  }
}