从嵌套函数JavaScript外部访问变量

时间:2019-03-12 10:24:06

标签: javascript html

我正在一个项目中,我必须获取一些值并将它们放入变量中。问题是我需要在另一个函数中调出这些变量,但我不能。我尝试使用全局作用域并从嵌套函数返回变量,但仍然无法正常工作。 这是youtube API的缩短代码

  //First Result
  
  function executeRequest(request) {
    request.execute(function(response) {
      console.log(response);
      let output = "";
      var x = 0;
      while (x < response.items.length){
      const subscriberItems = response.items[x];
      const channId = subscriberItems.snippet.resourceId.channelId;
      output += `${channId} `;
     x++;
      }
      var str = output;
  var res = str.split(" ");
  var chanId = "(" + res + ")";
  nId = chanId.includes("UCwdo8-3UrfZ9scHPl0m4Ysg");
    });
  }
  
  //Second Result
  
    function executeRequest1(request1) {
    request1.execute(function(response1) {
      console.log(response1);
      let output1 = "";
      const rateItems = response1.items[0];
      const rateId = rateItems.rating;
    });
  }
<html>
<body>
<button id="execute-request-button">Authorize</button><br/>

Check: <button id="checkBtn" type="submit" disabled>Get Reward</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script async defer src="https://apis.google.com/js/api.js" 
        onload="this.onload=function(){};handleClientLoad()" 
        onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
</body></html>
我需要它以这种方式工作

//Should work like this
var result1 = nId //result of fisrt function
var result2 = rateId //result of second function
if(result1 == "something" || result2 == "something"){
document.getElementById('checkBtn').disabled = false;
}

请原谅我命名函数和变量的方式,并且我知道第一个函数的结果是布尔值。请帮助我

0 个答案:

没有答案