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

  • SEARCH
  • Home
  • 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 6593557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:41:49+00:00 2026-05-25T17:41:49+00:00

My code sends requests to Twitter for search data gets responses in the from

  • 0

My code sends requests to Twitter for search data gets responses in the from of JSON. After getting the JSON, it stores the count of responses that match a certain condition in an array.

Here is the code that makes the call to the function that queries Twitter.

$(document).ready(function() {
...
graph = new HighCharts.chart({
    chart: {
        events: {
            load: function() {
                console.log("events.load");

                var that = this;
                var update = function() {
                if (polling) {
                    console.log("update()");
                    // The least index series will be nearest the x-axis
                    var stackNumber = 0;
                    var numTweets = updateValues();
                    console.log(numTweets + "");
                    for (var i = 0, currentSeries = that.series; i < currentSeries.length; i++) {
                        var x = (new Date()).getTime(), // current time
                        y = numTweets[i];

                        stackNumber += y;
                        currentSeries[i].addPoint([x, y], true, true);
                    }
               }
          }
          // set up the updating of the chart each second
          var series = this.series[0];
          setInterval(update, 1000);
     }
}

(I’m probably missing some brace somewhere in the code-paste here, but I know for sure that my problem isn’t related to a missing brace)

And here is the function that actually queries Twitter using a series of jQuery calls. The updateValues() function (which is outside the document-ready section) goes as follows:

    function updateValues() {
        var url = "http://search.twitter.com/search.json?callback=?&q=";
        var cls = "Penn_CIS240"
        var query = "%23" + cls;
        var voteCount = [0,0,0,0];

        // create an array of zeros
        //for (var i = 0; i < 4; i++)
        //    voteCount.push(0);

        $.getJSON(url + query, function(json){
            console.log(json);
            $.each(json.results, function(i, tweet) {
                var user = tweet.from_user_id;

                if (user % 2 == 0) {
                    voteCount[0] += 1;
                }
                else if (user % 3 == 0) {
                    voteCount[1] += 1;
                }
                else if (user % 5 == 0) {
                    voteCount[2] += 1;
                }
                else {
                    voteCount[3] += 1;
                }
                console.log("updateValues() -> getJSON -> each -> voteCount = " + voteCount);
            });
            console.log("updateValues() -> getJSON -> voteCount = " + voteCount);
        });
        console.log("updateValues() -> voteCount = " + voteCount);

        return voteCount;
    }

What is happening is that the variable voteCount is getting incremented properly inside the jQuery calls. However, outside of the calls, it is getting reset. So the log outputs look something like this:

updateValues() -> getJSON -> each -> voteCount = [1,0,0,0]
updateValues() -> getJSON -> voteCount = [1,0,0,0]
updateValues() -> voteCount = [0,0,0,0]

Does this problem have to do with jQuery’s asynchronous calls, and I’m having interesting variable modification conflicts? Or is it something else?

  • 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-05-25T17:41:49+00:00Added an answer on May 25, 2026 at 5:41 pm

    When you use asynchronous callbacks in JavaScript, they execute later… asynchronously. So if you have:

    var x = 5;
    console.log("before getJSON", 5);
    $.getJSON("/some/url", function (json) {
        x = 10;
        console.log("inside callback", x);
    });
    console.log("after getJSON", x);
    

    the output will be

    before getJSON 5
    after getJSON 5
    inside callback 10
    

    Any code you want to execute after the request returns must be inside the callback; putting it physically “after” the $.getJSON call will not suffice. You should think of $.getJSON as “firing off” the JSON-getting process, then immediately returning to you; only later, when your script is done executing normal code and the server has responded, will the JavaScript event loop say “hey I got a response and am idle; time to call that callback that was waiting on the response!”


    Because of this, your updateValues function will need to accept a callback of its own in order to notify its own caller that the values have been updated; just calling updateValues will only fire off the value-updating process, and the values won’t be updated later until that idle time. Something like:

    function updateValues(onUpdated) {
        var url = "http://search.twitter.com/search.json?callback=?&q=";
        var cls = "Penn_CIS240"
        var query = "%23" + cls;
        var voteCount = [0,0,0,0];
    
        $.getJSON(url + query, function (json) {
            // use of json to update voteCount ellided
    
            onUpdated(voteCount);
        });
    }
    

    Then calling code uses it as

    updateValues(function (voteCount) {
        // use the updated vote counts inside here.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that requests some JSON from an API. When data is
I have code that sends emails to users from a signup webpage. Occasionally, users
Goal: Website1 sends Website2 user data through http requests. Problem: Website2 is ensured that
I have some code that sends multiple ASIHTTPRequests to upload and download data in
I've found this small code that sends email to gmail users. I'd like the
My code works (yeah!) which sends json to a server.. would appreciate any thoughts
After checking for both fread and fopen with the search-command php fread php code
I have an HTML page with a JavaScript code that sends AJAX request. The
I'm using this example in my code, for some reason it sends requests as
I'm using a CMS that sends all requests to an index.php file using the

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.