views:

37

answers:

3

I'm just getting started with jQuery. I was using it to make a photo gallery that pulls photos and their information from Flickr. The script is shown below

<html>
<head>
    <title>Flickr Test</title>

<script src="http://rhol.squarespace.com/storage/js/jquery.js" type="text/javascript"></script>
<script src="http://rhol.squarespace.com/storage/js/fancybox/jquery.fancybox-1.3.1.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="http://rhol.squarespace.com/storage/js/fancybox/jquery.fancybox-1.3.1.css" media="screen" />



<script>
$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&amp;api_key=db05e5f5e5c2151218cc06545aaae27e&amp;photoset_id=72157624591740770&amp;format=json&amp;jsoncallback=?",
    function(data2){
    $("#setName").text(data2.photoset.title._content);
    $("#setDesc").text(data2.photoset.description._content);
    });
    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&amp;api_key=db05e5f5e5c2151218cc06545aaae27e&amp;photoset_id=72157624591740770&amp;format=json&amp;jsoncallback=?",
        function(data1){
            $.each(data1.photoset.photo, function(k,item){
                var baseurl = 'http://farm'+item.farm+'.static.flickr.com/'+item.server+'/'+item.id+'_'+item.secret;
                $("<tr><td id=\"image"+k+"\"><a> </a></td><td id=\"desc"+k+"\"></td></tr><tr />").appendTo("#images");
                $("#image"+k+" > a").attr("href",baseurl + "_b.jpg").attr("rel","mylightbox[photoset]").attr("class","gallery");
                $("<img/>").attr("src",baseurl+'_m.jpg').appendTo("#image"+k+" > a");
                $("a.gallery").fancybox({'titlePosition':'inside'});
                $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&amp;api_key=db05e5f5e5c2151218cc06545aaae27e&amp;photo_id="+item.id+"&amp;format=json&amp;jsoncallback=?",
                function(data2){
                    $("#desc"+k).text(data2.photo.description._content);
        $("#image"+k+" > a").attr("title",data2.photo.description._content);
                });
            });
        });
</script>

</head>
<body>
    <a name="top"></a><br />
    <h1 id="setName"></h1>
<h3 id="setDesc"></h3>
    <br />
<table id="images">
</table>
    <a href="#top">top</a>
</body>

My problem is that this script seems to lock up the browser if there are too many photos. What I wanted to do was have it load say 50 photos and then have another page with another 50. However, I wasn't sure how to make each start with the 50th item in the json object instead of the first. I was able to make it stop at 50 by having it return false when k == 50. If someone could give me an example of how to do this using each I'd appreciate it. Thanks in advance.

+1  A: 
.slice(a,b).each()
Crayon Violent
I attempted to add in $.slice(50,100).each(...) but it then fails to run. The photos don't show up and Firebug shows it as an error.
rhololkeolke
+1  A: 

As you have a Javascript array rather than a jQuery object, you use the slice method in the array class instead of the slice method in jQuery:

$.each(data1.photoset.photo.slice(50, 100), function(k,item){
Guffa
+1  A: 

I think what you are trying to do is covered by the Flickr API docs on flickr.photosets.getPhotos. Check the per_page option.

Andy Atkinson
Your right that the api covers this (I should have checked that first) but since the other answer explains how to start each from a different point I'm accepting that as the answer.
rhololkeolke
+1 for looking closer to the data source. Just limiting the array in the script is the simplest solution, but looking into limiting what you fetch should be worth the extra work.
Guffa