Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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中的函数获取值_Javascript - Fatal编程技术网

使用全局变量通过javascript中的函数获取值

使用全局变量通过javascript中的函数获取值,javascript,Javascript,因此,投票在函数外部是未定义的,但在函数内部是正确定义的 我想知道如何在函数之外使用data.value。我唯一能想到的就是使用一个全局变量,然而,它并没有像预期的那样工作 编辑:需要功能之外的IP var voted; $.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){ voted = data.host; console.log(voted); }); console.log(voted);

因此,
投票
在函数外部是
未定义的
,但在函数内部是正确定义的

我想知道如何在函数之外使用
data.value
。我唯一能想到的就是使用一个全局变量,然而,它并没有像预期的那样工作

编辑:需要功能之外的IP

var voted;

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
  voted = data.host;
  console.log(voted);
});

console.log(voted);
你可以用这种方式

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
  voted(data.host);
});

function voted(ip) {
  ip_voted = ip_array.indexOf(ip);

  if (ip_voted > -1) {
    window.location.href = "results";
  }
}


if (choice && ip == '123123123') {

}
或者留下一个同步电话(我认为不可取)


它不适合您的原因是对
getJSON
的调用是一个异步调用。从某种意义上说,它意味着它从当前流中剥离出来,并在某个地方独立执行。请看我在下面代码中添加的注释:

async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default).      If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().
您需要添加一个函数来继续您的流程,如下所示:

var voted;

//getJSON is executed right away - async
$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
  //This callback function executes when the remote request responds. The timeframe is variable.
  voted = data.host;
  console.log(voted);
});

//Gets executed right away - doesn't wait for getJSON to get the remote response.
//voted is probably still undefined.
console.log(voted);

如果您确实需要在全局变量中使用数据,您可以“等待”响应,但是我强烈建议不要这样做:

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
    voted(data.host);
});

function voted(ip) {
  ip_voted = ip_array.indexOf(ip);

  if (ip_voted > -1) {
    window.location.href = "results";
  }

   verify(ip);
}

function verify(ip) {
    if (choice && ip == '123123123') {

    }
}

你不能。使用回调。您如何期望由非同步任务获取的值在同步流中可用?如果我能得到更全面的答案,那就太好了。我不擅长javascript。你需要在回调中包装需要
数据的代码,也就是
函数(数据){/*here*/}
nononno,从不设置
async:true
!如何将它从所有函数中取出并放入变量中?据我所知,您这样做的唯一方法是使用同步ajax调用,但正如前面所说,这是不可取的。如何将其放入函数外部的变量中?取决于您的意思-在任何情况下,您都需要等待异步调用的响应,然后才能使用该值-即使该值存储在全局变量中。基本上,我需要在函数外部的代码中的其他位置(在if条件下)使用IP。如何在不将IP放入变量的情况下执行此操作。何时执行此if语句?最好你应该推迟执行,直到你有了你需要的数据。你可以看看我更新的问题,它提供了代码。
$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
    voted(data.host);
});

function voted(ip) {
  ip_voted = ip_array.indexOf(ip);

  if (ip_voted > -1) {
    window.location.href = "results";
  }

   verify(ip);
}

function verify(ip) {
    if (choice && ip == '123123123') {

    }
}
var host, value;
var responseArrived = false;

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
     host = data.host;
     value = data.value;
     responseArrived = true;
});

while( !responseArrived ) {
   //NOT A GOOD IDEA!
   //Will continue to loop until the callback of getJSON is called.
   //NOT A GOOD IDEA!
}

console.log(host);
console.log(value);