Random a channel

I’m sorry for my english. I use traductor
I am using javascript only.
Well, I managed to get the channels are live.
But I can not make random from all channels and select only one.
I tried putting a comma after the channels, then just separate them using split(), and so turn them into an array.
But that does not work I do not know why.
Could help me or tell me how I can get a random channel the channel list in live.

Thanks to all

You’re only loading one channel at a time. You need to use /streams?channel= to load them all at the same time, so that you can choose one.

function getRandomStream(channels) {
    return $.getJSON("https://api.twitch.tv/kraken/streams?channel=" + channels.join(",") + "&callback=?").then(function (data) {
        return data.streams.length ? data.streams[Math.floor(Math.random() * data.streams.length)] : null;
    });
}

getRandomStream(['maddenamy','lvpes2','lulosguat','gamerstudiotv','yosoyyazz','esl_spain']).then(function (stream) {
    $('#name').text(stream ? stream.channel.display_name : "No streams live");
});
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.