0

さて、つるのつぶやきを取得し、iFrame を介してビデオを表示する関数があります。これはうまく機能しますが、唯一の問題は、一度にすべてをリストしたくないことです.1つだけを表示し、終了したら次のものを表示したいだけです. おそらく非常に単純ですが、コードを長い間見てきたので、今では正しく考えることができません。助けてください。

         function getTweets() {
                    var url='http://search.twitter.com/search.json?callback=?&q=givingvine';
                    $.getJSON(url,function(json){

                    //setup an array to buffer output
                    var output = [];

                    //a for loop will perform faster when setup like this
                    for (var i = 0, len = json.results.length; i < len; i++) {
                   //instead of appending each result, add each to the buffer array
                   output.push('<p>' + '<iframe class="video" src="' + json.results[i].text.substr(json.results[i].text.indexOf("http")) +'" frameborder="0" width="1080" height="800" style="margin:-155px -100px -130px -60px;"></iframe>' + '</p>');

                    }

                //now select the #results element only once and append all the output at once, then slide it into view
                  $.each(output, function (intIndex, objValue) {
                    $("#results").html(objValue);
                    });
                });
                }
                 //set an interval to run the getTweets function (30,000 ms is 5 minutes), you can cancel the interval by calling clearInterval(timer);
                 var timer = setInterval(getTweets, 10000);
                clearInterval(timer);
                //run the getTweets function on document.ready
                getTweets();

            });
4

2 に答える 2

1

編集:

次のようなものが必要なようです。

output.push('<p>' + '<iframe id="iframe' + i.toString() + '"  class="video" src="' + json.results[i].text.substr(json.results[i].text.indexOf("http")) +'" frameborder="0" width="1080" height="800" style="margin:-155px -100px -130px -60px;"></iframe>' + '</p>');

                    }

次に、ここに:

              $.each(output, function (intIndex, objValue) {
                $("#results").html(objValue);
                //Some Timer code here;
                //Remove previous iframe, add a new iframe
                $('#results').empty();
                });

ただし、forloop を変更して出力に src を設定し、foreach で次のようにすることもできます。

              $.each(output, function (intIndex, objValue) {
                $("#iframe").attr('src', objValue)
                //Some Timer code here;
                });

セットアップで iframe を一度に 1 つずつ表示する場合:

each() 関数を使用することもできます。

$.each(output, function (intIndex, objValue) {
     $("#results").html(objValue);
});

以下の代わりに:

$("#results").html(output);
于 2013-03-01T17:07:13.057 に答える
1

私の理解が正しければ、結果を出力に保存し、最初の結果のみを #results に追加します。次に、ユーザーが何かを行ったときに別のユーザーを追加しますか?

現在の「場所」を変数に保存し、ユーザーが次の場所を追加したいことをしたときに、それを1ずつ増やすことをお勧めします

何かのようなもの

  var output =[],currentKey =0;
  function getTweets() {
                var url='http://search.twitter.com/search.json?callback=?&q=vine';
                $.getJSON(url,function(json){

                //setup an array to buffer output


                //a for loop will perform faster when setup like this
                for (var i = 0, len = json.results.length; i < len; i++) {
               //instead of appending each result, add each to the buffer array
               output.push('<p>' + '<iframe class="video" src="' + json.results[i].text.substr(json.results[i].text.indexOf("http")) +'" frameborder="0" width="1080" height="800" style="margin:-155px -100px -130px -60px;"></iframe>' + '</p>');

                }

            //now select the #results element only once and append all the output at once, then slide it into view
                $("#results").html(output[currentKey]);
            });
            }

         getTweets();

        $('#next').click(function(){
          currentKey +=1;
          if(currentKey  < output.length){
         $('#results').append(output[currentKey]);   
       }
       });

上記の例では、クリックすると次のアイテムが結果に追加される id=next を持つある種のボタンがあることを前提としています。

  <button id="next">Next Video</button>

現在のビデオを置き換えたい場合は、使用します

        $('#results').html(output[currentKey]);   
       //  instead of
       $('#results').append(output[currentKey]);   
于 2013-03-01T17:02:59.087 に答える