javascript - Inconstant panel placement with jquery + bootstrap -
i building custom rss dashboard , iterating on array of feeds , throwing them google service.
but when @ panels not in order consistently.
code:
jquery
var rssarray = [ 'https://blog.malwarebytes.org/feed/', 'http://krebsonsecurity.com/feed/', 'http://motherboard.vice.com/rss?trk_source=motherboard', 'https://www.trustwave.com/rss.aspx?type=twblog', 'http://threatpost.com/feed/', 'https://news.ycombinator.com/rss', 'http://feeds.trendmicro.com/trendmicrosimplysecurity?format=xml', 'https://securityintelligence.com/topics/vulnerabilities-threats/feed/', 'https://securityintelligence.com/topics/x-force/feed/' ] (var i=0; < rssarray.length; i++){ $.ajax({ url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&callback=?&q=' + encodeuricomponent(rssarray[i]), datatype: 'json', success: function(data) { // console.log(data) var feedbody = $('<div>', {class: 'panel-body'}) var feedhead = $('<div>', {class: 'panel-heading', html: '<h3 class="panel-title text-center">'+capitalisefirstletter(data.responsedata.feed.title)+'</h3>'}) var contentholder = $('<ul>', {class: 'list-group'}) var feed = $('<div>', {class: 'col-xs-6 col-md-4 panel panel-warning'}).append(feedhead).append(feedbody).append(contentholder) $.each(data.responsedata.feed.entries, function(key, value){ if (value.contentsnippet === "comments"){ var thehtml = '<a href="'+value.link+'" target="_blank" class="list-group-item"><h5 class="list-group-item-heading">'+value.title+'</h5></a>'; } else { var thehtml = '<a href="'+value.link+'" target="_blank" class="list-group-item"><h5 class="list-group-item-heading">'+value.title+'</h5><p class="list-group-item-text">'+value.contentsnippet+'</p></a>'; } feedbody.append(thehtml) }); $("#content").append(feed) } }) }
html
<div class="row"> <div id="content"> </div> </div>
examples of issues:
you're kicking off series of ajax calls, will execute unknown amount of time. when complete call success callbacks.
if first request takes 5 seconds, third request takes 2, third request rendered before first.
if need ensure these requests have completed before rendering contents, use $.when
create promise (jquery deferred actually) , render data when has resolved.
Comments
Post a Comment