Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:55:03+00:00 2026-06-05T21:55:03+00:00

hi i’m having a problem with this code. my problem is that the first

  • 0

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.

  $.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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:55:05+00:00Added an answer on June 5, 2026 at 9:55 pm

    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. 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. In this case, your callback starts function(data) on the third line.

    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. 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. 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.

    I realize this is tricky to grasp at first, but once you get this, you’ll get this whole ajax thing.

    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
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.