javascript - Random photos from Flickr JSON feed -
i use query parse json feed flickr account , inject code of latest 12 photos feed in div named #photos
<script type="text/javascript"> // set variables needed query var url = "http://api.flickr.com/services/feeds/photos_public.gne"; var id = "<?php echo get_option('of_flickrid') ?>"; var jsonformat = "&lang=en-us&format=json&jsoncallback=?"; var ajaxurl = url + "?id=" + id + jsonformat; // last photos of flickr account, parse html code $.getjson(ajaxurl, function(data){ var htmlstring = '<a href="<?php echo get_option('of_flickr') ?>" target="_blank"><h1><?php echo get_option('of_photostext') ?></h1></a><br class="clear"/>'; // start cycling through our array of flickr photo details $.each(data.items, function(i,item){ // want ickle square thumbnails var sourcesquare = (item.media.m).replace("_m.jpg", "_s.jpg"); // here's piece html htmlstring += '<a href="' + item.link + '" target="_blank">'; htmlstring += '<img title="' + item.title + '" src="' + sourcesquare; htmlstring += '" alt="'; htmlstring += item.title + '" class="rounded"/>'; htmlstring += '</a>'; if(i === 11){ return false; } }); // pop our html in #images div $('#photos').html(htmlstring); }); // end getjsoon </script> but need load 12 random photos rather latest 12, how can this?
dump them array, splice out elements @ random. since .splice() removes elements original array, won't have worry extracting same item twice.
$.getjson(ajaxurl, function(data){ var items = data.items, // array extract = [], // array max = 12; // number of items remove (var i=0; i<max; i++) { var rand = math.floor(math.random()*items.length); // random index extract.push(items.splice(rand,1)[0]); // splice() returns array }; // 'extract' });
Comments
Post a Comment