0

I am working on codepen on a project. Its actually freecodecamp project. In the code below, I am using twitch api to get data (i.e icon, url, status) about few channels. I have created a function ( mymain(key)) which takes one argument i.e. a key to decide the list of channels you want to see. If 1 is passed as argument it will show all channels. If 2 is passed as argument it will show only online channels. If 3 it will show only offline channels. It is getting data perfectly using jquery but the problem comes when I try to display it using key. All the three click statements in the bottom are always running and the buttons in html do not work.

//channel names variable initialization
    var channelNames = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "habathcx", "RobotCaleb", "noobs2ninjas"];
    //parts of url to get data    
    var url1 ="https://wind-bow.gomix.me/twitch-api/channels/", url2 = "https://wind-bow.gomix.me/twitch-api/streams/", urlend="/?callback=?", htm="";

    //function mymain
    function mymain(key){
      channelNames.forEach(function(name){
        $.getJSON(url1+name+urlend,function(data){
          var Name = name, status="", icon = data.logo, channelWeb = data.url;
          var urltemp2 = url2+name+urlend, status = data.status,myvar = 1;//myvar is used to check if a channel is online
          $.getJSON(urltemp2,function(data){
            if (data.stream===null){
              status="Offline";
              myvar = 0;
            }
    //The code inside all the conditions is same.
    //for All channels this condition works
            if (key===1){
             htm = htm +"  <div class=\"box row\"><div class=\"images col-xs-1\"><img class=\"img-responsive img-circle\" src ="+icon+"></div><div class=\"text-left col-xs-4\"><a target=\"_blank\" href=\""+channelWeb+"\">"+Name+"</a></div><div class=\"text-center col-xs-7\">"+status+"</div></div><hr>"; 
            }

    //this condition is for online channels
            else if (key===2 && myvar===1){
            htm = htm +"  <div class=\"box row\"><div class=\"images col-xs-1\"><img class=\"img-responsive img-circle\" src ="+icon+"></div><div class=\"text-left col-xs-4\"><a target=\"_blank\" href=\""+channelWeb+"\">"+Name+"</a></div><div class=\"text-center col-xs-7\">"+status+"</div></div><hr>";
              
            }

    //this condition is for offline channels
        else if(status==="Offline"){
            htm = htm +"  <div class=\"box row\"><div class=\"images col-xs-1\"><img class=\"img-responsive img-circle\" src ="+icon+"></div><div class=\"text-left col-xs-4\"><a target=\"_blank\" href=\""+channelWeb+"\">"+Name+"</a></div><div class=\"text-center col-xs-7\">"+status+"</div></div><hr>";
            }
            $(".channelsInfo").html(htm);
          });
        });
      });
    }
    $(document).ready(mymain(1));
    $(".Online").click(mymain(2));
    $(".All").click(mymain(1));
    $(".Offline").click(mymain(3));
body{
  background-color:#222222;
  color:#AAAAAA;
}
#title{
  background-color:#4a4a4a;
  margin-top:3%;
}
img{
  width:50px;
}
.images{
 border-right: 3px solid grey;
}
.All:hover{
  color:blue;
}
.channelsInfo{
  background-color:#192345;
}
.box{
  margin:1%;
  padding:1%;
}
h1{
  padding:3%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div id="title" class="text-center ">
    <h1>Twitch Streamers</h1>
  </div>
  <div class="row text-left">
    <div class="All col-xs-1">
     <button> All</button>
    </div>
    <div class="Online col-xs-1">
      <button>   Online</button>
    </div>
    <div class="Offline col-xs-1">
      <button onclick="mymain(2)">Offline</button>
    </div>
    <div class="col-xs-9"></div>
  </div>
  <div class="channelsInfo">
  </div>
</div>
  • `$(".Online").click(mymain(2));` **calls** `mymain(2)` and passes its return value into `click`, exactly the way `foo(bar())` *calls* `bar` and passes its return value into `foo`. You need to give `click` a function to call, rather than calling the function yourself. In this case, you'd do that with a wrapper function (`$(".Online").click(function() { mymain(2); });`) or `Function#bind` (`$(".Online").click(mymain.bind(null, 2));`). (This applies to all the others as well.) – T.J. Crowder Jul 26 '17 at 17:48
  • [Why does click event handler fire immediately upon page load?](https://stackoverflow.com/questions/7102413/why-does-click-event-handler-fire-immediately-upon-page-load) – t.niese Jul 26 '17 at 17:48
  • Thanks! But there is another problem. Online and offline buttons are not working – AsksBadQuestions Jul 26 '17 at 17:51
  • @T.J.Crowder Buttons are also not working – AsksBadQuestions Jul 26 '17 at 17:53
  • Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Questions on SO are meant to be targeted on a single specific problem. Also, there are no buttons in your question. – T.J. Crowder Jul 26 '17 at 17:55
  • @T.J.Crowder Buttons are in the codepen link – AsksBadQuestions Jul 26 '17 at 18:02
  • The full content of your question must be **in** your question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to go off-site to help you. Put a [mcve] **in** the question, ideally using Stack Snippets (the `<>` toolbar button) to make it runnable. More: [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Jul 26 '17 at 18:02
  • @T.J.Crowder Added the snippet – AsksBadQuestions Jul 26 '17 at 18:12
  • With respect, please see my previous comment: *"Questions on SO are meant to be targeted on a single specific problem."* Your main question above is answered by the linked dupetarget. If you have another questino, by all means, after doing thorough research and debugging, post another question in keeping with [*How do I ask a good question?*](/help/how-to-ask) People (including me!) will be happy to help. – T.J. Crowder Jul 26 '17 at 18:15
  • @T.J.Crowder Thanks Sir! I am so sorry. You are a good person TJ. – AsksBadQuestions Jul 26 '17 at 18:17

0 Answers0