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 6355157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:42:30+00:00 2026-05-24T22:42:30+00:00

I am trying to get a total from the return of multiple ajax requests,

  • 0

I am trying to get a total from the return of multiple ajax requests, I use sync right now to make it work, since I think that would be a much better solution.

Here is my Coffee

get_total = (trend, duration) ->
  total = 0
  for keyword in trend.search_terms
    url = "http://otter.topsy.com/search.json?q=#{keyword}&window=#{duration}"
    $.ajax
      url: url
      async: false
      success: (data) ->
        total += data.response.total
  total

Which compiles nicely to

  get_total = function(trend, duration) {
    var keyword, total, url, _i, _len, _ref;
    total = 0;
    _ref = trend.search_terms;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      keyword = _ref[_i];
      url = "http://otter.topsy.com/search.json?q=" + keyword + "&window=" + duration;
      $.ajax({
        url: url,
        async: false,
        success: function(data) {
          return total += data.response.total;
        }
      });
    }
    return total;
  };

Is there a way to have the total work without using synchronous js.

I have been experimenting with the $.when().then() but it is causing issues when the size of the requests is dynamic.

  • 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-24T22:42:31+00:00Added an answer on May 24, 2026 at 10:42 pm

    I don’t know CoffeeScript, so here is a pure jQuery solution:

    You cannot return a value from get_total without making synchronous calls. What you can do is call a callback, once all the requests have finished.

    This example makes use of jQuery’s Deferred objects [docs]:

    function get_total(trend, duration, callback) {
        var deferreds = [], total = 0;
    
        for(var i = 0, l = trend.search_terms.length; i < l; i++) {
            deferreds.push($.get("http://otter.topsy.com/search.json?q=" + trend.search_terms[i] + "&window=" + duration, function(data) {
                total += data.response.total;
            }));
        }
    
        $.when.apply($, deferreds).then(function() {
            callback(total);
        });
    }
    

    Usage:

    get_total(trend, 200, function(total) {
       // now execute the code that needs `total`
    });
    

    If you need get_total to return the value, then you have to make synchronous calls. In general this is a bad idea, especially when you are making multiple requests. It will freeze the browser’s UI. Better you restructure your code to work with callbacks.

    Update: I just read the last sentence of your question. You you can pass a dynamic number of arguments to a function using .apply() [MDN].

    Update2: Of course if you have control over the service, you should make it accept multiple keywords to avoid multiple Ajax requests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a total record count from the below Method using
I am trying to get the total count of rows from Sqlite DB. Following
Right now I'm trying to serve CSS and JS files from a server that
Hello I am trying to get the entire total page size with HttpWebRequest from
So, I'm trying to figure out the total amount of return from an investment
I'm trying to get total salary for the whole company by: NSNumber *totalSalary =
I'm trying to find a way to get the total number of child nodes
I am trying get Struts 2 and Tiles to work and I am using
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {

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.