www.webdeveloper.com
Results 1 to 3 of 3

Thread: Stopping a unique for i=0 loop at 5 entries

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Stopping a unique for i=0 loop at 5 entries

    I've borrowed some code from various sources to get a delicious JSON feed onto my website. The problem is it this loop gives me the entire feed that is available, where I would only like to have 5 current entries. I understand how to do this in a for i=0 loop, however there is an extra bit of code that I do not understand within the loop. What do I need to do to end this loop at 5 entries instead of the 15 or so it generates?

    Code:
    <script>
    
    <!--To be honest I have no idea what this does. I sampled code from 3 different resources to make this work, and the author of this code didn't attempt to explain what is going on here. It doesn't work without it. I'm fairly certain it parses the JSON file so that it is readable. -->
    
    Function.prototype.bg = function(ms){
    	this.PID = setInterval(this,ms);
    	return this;
    }
    Function.prototype.kill = function(){
    	clearInterval(this.PID)
    }
    String.prototype.onReady = function(func){
    	var str = this;
    	return function(){
    		try{
    			eval("var res=("+str+")");
    			if(res){
    				arguments.callee.kill();
    				func()
    			}
    		}catch(e){
    		}
    	}.bg(10)
    }
    
    <!--This is the function that gets the JSON code from the delicious servers. It simply takes the link to the feed you want and appends our "user" variable that we input so that the feed for that particular user is displayed. -->
    
    
    function load(){
    	var user = "delicioususer"
    	"Delicious.posts".onReady(feed_onload);
    	var s = document.createElement("script");
    	s.src = "http://del.icio.us/feeds/json/"+user;
    	s.charset = "utf-8";
    	document.body.appendChild(s);
    }
    
    <!--This function creates an unordered list, and then runs a loop for each of the post retrieved above. It sets up and styles the img, a, and li objects that are created. -->
    
    function feed_onload(){
    	var ul = document.createElement('ul')
    	for (var i=0, post; post = Delicious.posts[i]; i++) {
    		var li = document.createElement('li')
    		var a = document.createElement('a')
    		a.style.marginLeft = '20px'
    		var img = document.createElement('img')
    		img.style.position = 'absolute'
    		img.style.display = 'none'
    		img.height = img.width = 16
    		img.src = post.u.split('/').splice(0,3).join('/')+'/favicon.ico'
    		img.onload = showImage(img);
    		a.setAttribute('href', post.u)
    		a.appendChild(document.createTextNode(post.d))
    		li.appendChild(img)
    		li.appendChild(a)
    		ul.appendChild(li)
    	}
    	document.getElementById('delicious').appendChild(ul)
    	delete Delicious.posts;
    }
    function showImage(img){ return (function(){ img.style.display='inline' }) }
    </script>

  2. #2
    Join Date
    Jul 2007
    Posts
    386
    Do a break;

    Code:
    if (i == 4)
        break;

  3. #3
    Join Date
    Jun 2010
    Posts
    2
    Thanks a lot. This solution will work. I wasn't sure if there was a way to do it within the actual syntax of the for loop itself.

    Still a beginner here

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles