Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Looping Function

    Hey, could anyone let me know how to get this function to loop?

    Code:
    $(".next").on('click', function () {
    
      var $projects = $("#projects");
    
      pid=$projects.data('id'); // reads the data id
    
      $projects.data('id', pid+1 ); // Increment and saves the data id  
    
      $projects.load("projects.html #div" + $projects.data('id') , function () {
    
      }); return false
    
    });
    This script basically counts through some divs on a page by incrementing a data id. Say there's 5 divs, it will count through them all but then when it gets to the 5th the code will end. Is there a way to get it loop back round to start counting from the beginning again?

    Demo

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,219
    Thanks
    23
    Thanked 606 Times in 605 Posts
    Is this what you want:
    Code:
    $(".next").on('click', function () {
    	var $projects = $("#projects");
    	pid=$projects.data('id'); // read
    	if(pid == 5) pid = 0;
    	$projects.data('id', pid+1 ); // increment and save
    	$projects.load("projects.html #div" + $projects.data('id'));
    	return false;
    });
    Please note the removal of the callback function that was not used for load().
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fantastic, thanks! (:

    Mind if I ask a quick question about the same piece of code? I don't want to annoy people by starting a few threads about the same thing.

    The content is loaded like this:

    Code:
    $(".load").on('click', function () {
    
        var $projects = $("#projects"),
    
            id = $(this).data('id');
    
        $projects.html("").data('id', id);
    
        $projects.load("projects.html #div" + id, function () {
    
        $("#loading").hide();
    
        $("#projects").show(); // I had to add this after adding the exit function to make sure the content could be viewed more than once, otherwise it would have stayed hidden.
    
        })
    });
    I'm exiting the content like so

    Code:
    $(".exit").on('click', function () {
    
        $("#projects").hide(); // This hides the content
    
    });
    This is a little exit button I put together, and I'm not sure if it's the correct way to do it. All it does is hide the content, not close it. This works fine, I'm not making sure there's not a proper way of 'closing' the content after it's been loaded.
    Last edited by davi; 04-16-2014 at 09:22 AM.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,219
    Thanks
    23
    Thanked 606 Times in 605 Posts
    For all practical purposes HIDE make the object GO AWAY. It is still in the coding, but it is not on the browser page.
    What you should be aware of is this:
    $projects.load("projects.html #div" + $projects.data('id') , function () {

    });

    You have a callback function that is empty. Guess it doesn't hurt, but it takes up coding bytes and increases page download time. Bad habit to get into. Remove the callback:
    $projects.load("projects.html #div" + $projects.data('id') );
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    $projects.load("projects.html #div" + id, function () {
    
        $("#loading").hide();
    
        $("#projects").show();
    
    })
    Sorry if I'm mistaken but I do need that function as I have the loading and projects events in there? I did try your code but ended up with some syntax errors. Not 100% on what you mean there!

    Thanks for the other answer though, I'll leave the exit as I had it!


  •  

    Posting Permissions

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