JQuery使用.each()通过输出缓冲区循环
好的,我有一个函数,可以获取vine tweets,然后通过iFrame显示视频。这很好。唯一的问题是我不想一次列出所有的,我只想显示一个,然后在完成后显示下一个。这可能很简单,但我已经看了很长时间的代码,现在我不能直接思考。请帮忙JQuery使用.each()通过输出缓冲区循环,jquery,loops,for-loop,Jquery,Loops,For Loop,好的,我有一个函数,可以获取vine tweets,然后通过iFrame显示视频。这很好。唯一的问题是我不想一次列出所有的,我只想显示一个,然后在完成后显示下一个。这可能很简单,但我已经看了很长时间的代码,现在我不能直接思考。请帮忙 function getTweets() { var url='http://search.twitter.com/search.json?callback=?&q=givingvine';
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 差不多
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]);
}
});
编辑:
听起来你可能想要这样的东西:
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) {
$("#iframe").attr('src', objValue)
//Some Timer code here;
});
但是,您也可以更改forloop以使用src填充输出,然后在foreach中执行以下操作:
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) {
$("#iframe").attr('src', objValue)
//Some Timer code here;
});
如果要在设置中一次显示一个iframe,请执行以下操作:
您可能希望使用以下各项功能:
$.each(output, function (intIndex, objValue) {
$("#results").html(objValue);
});
取代:
$("#results").html(output);
这就是我想要的,我不想要一个按钮,我希望它在一个特定的间隔后切换,然后用一个窗口替换click函数//10秒时间间隔应该存储在一个变量中,这样你可以在达到项目长度时终止它。是的,这接近我想要的。我将添加什么来使iframe切换到数组中的下一个?为此,您需要通过其类或向标记添加ID属性来选择iframe,而不是选择结果并应用html。比如$'iframe'.htmlobjValue;我对JQuery真的不是很在行,如果在for循环中生成iframe,我怎么能做到这一点呢?
$("#results").html(output);