jQuery-函数全局变量

jQuery - Function Global Variable

本文关键字:全局变量 函数 jQuery-      更新时间:2023-12-13

基本上,函数atm使它,所以当您加载脚本时,它将显示下一个和上一个按钮以及流。然后,如果你点击next,它会把你带到列表中的第二个流。通过这样做,它将计数器增加1,然后使用计数器调用函数showStream(counter)。我想这样做,当你调用函数时,如果你点击上一个,计数器小于0,比如-1,它只会显示流#100,如果你再次点击上一次,它会显示#99。此外,我还需要确保,如果你按下下一步,通过100号,它会把你带到第1位。把它想象成没有开始也没有结束,只有一个巨大的循环。

有什么想法吗?

功能:

  function showStream(videoIndexToShow) {
      if(videoIndexToShow >= streams.length-1) {
         jQuery("#next").hide();
       }
       else {
        jQuery("#next").show();
       }
       if(videoIndexToShow <= 0) {
         jQuery("#prev").show();
       } else {
        jQuery("#prev").show();
       }
       jQuery.each(streams, function (index, item) {  
           // $stream = <stream number>;
           // if(index == $stream){ 
           // } else {
           if(index == videoIndexToShow) {
            console.log(videoIndexToShow, index);
           jQuery("#content").empty();
           jQuery("#content").html('<object style="margin-left:0.5%;" type="application/x-shockwave-flash" height="378" width="620" id="live_embed_player_flash" data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' + item.channel.display_name+ '&auto_play=false&start_volume=25" /></object><iframe frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel=' + item.channel.display_name +'&amp;popout_chat=true" height="378" width="350"></iframe>');
           }
           // }
      });
  }

其他代码:

  var streams = null;
  jQuery.getJSON("https://api.twitch.tv/kraken/search/streams?q=path%20of%20exile&limit=100&callback=?", function (data) {
      streams =  data.streams;
      showStream(0);
  });
  var globalCnt = 0;
  jQuery("#next").click(function() {
      showStream(++globalCnt);
  });
  jQuery("#prev").click(function() {
      showStream(--globalCnt);
  });

如果可以让它而不是100,只要当它通过streams.length时就可以了。它将在1重新启动,或者如果它在0并且你按下上一步,它将把你带到streams.length.不确定这是否可能:)

但你知道我该怎么做吗?

点击事件:

  jQuery("#prev").click(function() {
      globalCnt = globalCnt + 1 > 100 ? 1 : --globalCnt;
      showStream(globalCnt);
  });

在调用showStream 之前只需使用三进制

globalCnt = globalCnt + 1 > 100 ? 1 : ++globalCnt;

价格更低:

globalCnt = globalCnt - 1 < 0 ? 100 : --globalCnt;