简体   繁体   English

无法获取我的变量以将其值保留在getJSON中

[英]can't get my variable to keep its value out of the getJSON

hi i'm having a problem with this code. 嗨,我有这个代码的问题。 my problem is that the first console.log(smile_per_sec) gives me the the value i need, but the second one gives me the value that i've given it when i declared the variable. 我的问题是,第一个console.log(smile_per_sec)给了我所需的值,但是第二个给了我声明变量时所给的值。

  $.getJSON( 
    twitter_api_url + '?callback=?&rpp=100&q=text::)',
    function(data) {
      $.each(data.results, function(i, tweet) {
        //console.log(tweet);
        // Before we continue we check that we got data
        if(tweet.text !== undefined) {
        //here we save the time of our happy tweet
          var date_tweet = new Date(tweet.created_at);
          per_sec[i] = date_tweet/1000;
          //console.log(per_sec[i]);
        }
      });

      //here we calculate how manny happy tweets the world tweets per second
        smile_per_sec = 100/(per_sec[0]-per_sec[98])
        $('#tweet_container').append(smile_per_sec + "<br>");   
          console.log(smile_per_sec);
          return smile_per_sec;
    }
  );
console.log(smile_per_sec);

it's probably an easy fix but stil your help would be verry much appreciated. 这可能是一个简单的解决方法,但是非常感谢您的帮助。

I'm assuming you want to compare it in the two places I notate below: 我假设您想在下面我注明的两个地方进行比较:

$.getJSON( 
  twitter_api_url + '?callback=?&rpp=100&q=text::)',
  function(data) {
    $.each(data.results, function(i, tweet) {
      //console.log(tweet);
      // Before we continue we check that we got data
      if(tweet.text !== undefined) {
        //here we save the time of our happy tweet
        var date_tweet = new Date(tweet.created_at);
        per_sec[i] = date_tweet/1000;
        //console.log(per_sec[i]);
      }
    });
    //here we calculate how manny happy tweets the world tweets per second
    smile_per_sec = 100/(per_sec[0]-per_sec[98])
    $('#tweet_container').append(smile_per_sec + "<br>");   
    ///////////////////////////////////////////////
    //  this is place one
    ///////////////////////////////////////////////
    console.log(smile_per_sec);
    return smile_per_sec;
  }
);
///////////////////////////////////////////////
// this is place two
///////////////////////////////////////////////
console.log(smile_per_sec);

Place two executes immediately, place one waits until the getJSON has occurred. 立即放置两个执行,放置一个等待直到发生getJSON。 This is called asynchronous programming. 这称为异步编程。 You can't use the value until you get it, and if you want something to "wait for the other thing to finish" you need to adopt flexibile async programming techniques. 您必须先获得该值,然后才能使用它;如果您希望某项“等待另一件事完成”,则需要采用灵活的异步编程技术。

Any time people talk about eventing or callbacks in javascript, that's what they're talking about. 每当人们谈论javascript中的事件或回调时,这就是他们所谈论的。 In this case, your callback starts function(data) on the third line. 在这种情况下,您的回调将在第三行启动function(data)

I feel the need to elaborate, let me do so: 我觉得有必要详细说明,让我这样做:

$.getJSON( ... );
console.log(smile_per_sec);

These two operate in effectively "the same time". 这两个有效地“同时”运行。 The thing that happens in getJSON gets stuck on the backburner till it's done, letting your page keep doing stuff. getJSON中发生的事情会卡在后台文件中,直到完成为止,让您的页面继续执行操作。 So that console.log just goes ahead and pops on out, but the back burner hasn't come back to the front, the water isn't boiling yet. 这样console.log才继续前进并弹出,但是后燃器尚未回到前燃器,水还没有沸腾。 So until the water boils (could be 10ms, could be 3 minutes) you can't do anything with the water, or the noodles in the water, or whatever. 因此,在水沸腾之前(可能是10毫秒,可能是3分钟),您将无法使用水或水中的面条做任何事情。

I realize this is tricky to grasp at first, but once you get this, you'll get this whole ajax thing. 我意识到一开始很难掌握这一点,但是一旦掌握了这一点,就会得到整个ajax的东西。

To answer the question of "how do I make it do everything when it finishes" the answer is to restructure your code like this: 要回答“完成后如何使它完成所有工作”的问题,答案是按照以下方式重组代码:

// Here we've made this a named function, and allowed the function to be more neatly encapsulated
function twitterCallback(data) {
  $.each(data.results, function(i, tweet) {
    //console.log(tweet);
    // Before we continue we check that we got data
    if(tweet.text !== undefined) {
      //here we save the time of our happy tweet
      var date_tweet = new Date(tweet.created_at);
      per_sec[i] = date_tweet/1000;
      //console.log(per_sec[i]);
    }
  });
  //here we calculate how manny happy tweets the world tweets per second
  smile_per_sec = 100/(per_sec[0]-per_sec[98])
  $('#tweet_container').append(smile_per_sec + "<br>");   

  console.log(smile_per_sec);
  return smile_per_sec;

  ///////////////////////////////////////////////
  //  this is where you put stuff that has to wait on twitter to return
  ///////////////////////////////////////////////

}

$.getJSON( 
  twitter_api_url + '?callback=?&rpp=100&q=text::)',
  twitterCallback
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM