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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:09:54+00:00 2026-05-31T06:09:54+00:00

I’ve been looking through forums all day, but I’m not figuring it out. I’m

  • 0

I’ve been looking through forums all day, but I’m not figuring it out.

I’m calling the Justin.TV-API to display a list of my “followed” streams and then doing a nested call to check which one of them is online.

Here is the code I’m wanting to use:

//Get Favorites as Object "favs"
        var viewers;
        $.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
            $.each(favs, function(key, value) {
                //For every Object, check if user is referenced in stream and therefore online
                $.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
                    viewers = streams[0].channel_count;
                });
                $('#result').append(favs[key].title + ' ' + key + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
            });
        });

I then came across the problems with getJSON and asychronisation and rewrote my script a couple of times:

first like this (replacing the $.each):

//Get Favorites as Object "favs"
        var viewers;
        $.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
            for (var key in favs) {
                //For every Object, check if user is referenced in stream and therefore online
                $.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
                    viewers = streams[0].channel_count;
                });
                $('#result').append(favs[key].title + ' ' + key + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
            }
        });

then like this (replacing getJSON with non-asynchronus ajax):

var favs;
        var streams;
        var viewers;
        $.ajax({
            url: 'http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?',
            async: false,
            dataType: 'json',
            success: function(favs) {
                for (var key in favs) {
                    $.ajax({
                        url: 'http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?',
                        async: false,
                        dataType: 'json',
                        success: function(streams) {
                            viewers = streams[0].channel_count;
                        }
                    });
                    $('#result').append(favs[key].title + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
                }
            }
        });

In no cases I am able to pass the viewers-variable (which is written in the nested api-call to determine if the stream is online and how many viewers it has) to

$('#result').append(favs[key].title + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');

My output is always undefined:

Day[9]: day9tv (undefined) Viewers
FollowGrubby: followgrubby (undefined) Viewers
OneMoreGame.TV: onemoregametv (undefined) Viewers

I have done several checks with appends and alerts – the JSON-responses are valid.

Who can help me out?

Thank you very much for your time.

  • 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-31T06:09:56+00:00Added an answer on May 31, 2026 at 6:09 am

    Ajax is asynchronous, anything that needs access to the viewers variable after it is populated needs to be inside of the complete callback of the ajax request.

    Deferred objects would be the way to go here, i’ll whip up a small demo.

    Update:
    http://jsfiddle.net/cMKkQ/

    didn’t need deferred objects, just restructured a little.

    //Get Favorites as Object "favs"
    $.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
        $.each(favs, function(key, value) {
            //For every Object, check if user is referenced in stream and therefore online
            $.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
                $('#result').append(value.title + ' ' + key + ': ' + value.login + ' (' + streams[0].channel_count + ') Viewers<br />');
            });
        });
    });
    

    ​Update:
    Another way to do it:

    var favorites = [];
    function processFavorites () {
        $("#result").empty();
        $.each(favorites,function(index,obj){
            // here we populate the results div
            $('#result').append(obj.title + ' ' + index + ': ' + obj.login + ' (' + obj.viewers + ') Viewers<br />');
        });
    }
    function getFavorites () {
        favorites = [];
        $.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
            var defArr = [];
            $.each(favs, function(key, value) {
                //For every Object, check if user is referenced in stream and therefore online
                defArr.push($.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
                    favorites.push({title:value.title,key:key,login:value.login,viewers:streams[0].channel_count});
                }));
            });
            $.when.apply($,defArr).always(function(){
                processFavorites();
            });
        });
    }
    // this runs the process of getting and processing favorites. Run it when you want.
    getFavorites();​
    

    however, the feeds are currently returning no results, i don’t think it’s related to this code.

    http://jsfiddle.net/cMKkQ/3/

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

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.