2013-03-01 55 views
0

好吧,我有一個函數可以獲取藤的推文,然後通過iFrame顯示視頻。這很好,唯一的問題是我不想一次列出它們,我只想要一個顯示,然後一旦完成顯示下一個。這可能很簡單,但我一直在看代碼很久,現在我無法思考。請幫忙。通過使用.each()的JQuery循環輸出緩衝區()

  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(); 

      }); 

回答

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 =下,單擊時附加下一項目結果,如

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

如果要替換當前的視頻,然後使用

 $('#results').html(output[currentKey]); 
     // instead of 
     $('#results').append(output[currentKey]); 
+0

這是有點我在找什麼,我不想要一個按鈕,我希望它切換後的指定ic間隔 – Christopher 2013-03-01 17:07:09

+0

然後用window.setInterval(function(){//從上面的代碼點擊函數}中取代「click」函數},10000); // 10秒間隔應該存儲在一個變量中,以便在達到項目長度時可以將其刪除。 – TommyBs 2013-03-01 17:08:10

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(); 
       }); 

你也可以但是改變for循環填充輸出與src然後在foreach中做這樣的事情:

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

如果你想在你設定了一個時間顯示的iframe一個:

您可能需要使用每個()函數:

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

在地方:

$("#results").html(output); 
+0

是的,這與我想要的很接近。我會添加什麼來使iframe切換到數組中的下一個? – Christopher 2013-03-01 17:11:48

+0

爲了取代選擇結果和應用html,您需要選擇iframe - 通過它的類或向標記添加ID屬性。所以像$('iframe')。html(objValue); – RenegadeMatrix 2013-03-01 17:22:44

+0

我真的沒有用JQuery那麼棒,我怎麼能夠在for循環中生成iframe呢? – Christopher 2013-03-01 17:31:48