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
Like Tree2Likes
  • 1 Post By rnd me
  • 1 Post By glenngv

Thread: Script Counting 1 Too Many

  1. #1
    New Coder
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Script Counting 1 Too Many

    I have these two functions which count forward and backwards between 6 divs.

    Code:
    $(document).on("click", '.next', function(event) { 
        var $projects = $("#projects");
        pid = $projects.data('id');
        if (pid == 6) pid = 1;
        $projects.data('id', pid + 1); 
        $projects.load("projects.html #div" + $projects.data('id'));
    });
    
    
    $(document).on("click", '.previous', function(event) { 
        var $projects = $("#projects");
        pid = $projects.data('id');
        if (pid == 1) pid = 6;
        $projects.data('id', pid - 1); 
        $projects.load("projects.html #div" + $projects.data('id'));
    });
    I added the
    Code:
    if (pid == 1) pid = 6;
    and
    Code:
    f (pid == 6) pid = 1;
    lines so it loops back round once you get to either the first or sixth div.

    For example, when you we're currently viewing the 6th div and clicked next, it would load div one.

    But if you're on the 6th div and click next it loads the 2nd div. And if you're on the 1st div and click previous, it loads the 5th.

    I understand why it's doing what it's doing (If your at 6 and click next the id's equal to one, but then it's adding one more because that's what the function does) but I'm unsure of a fix.

    Any one know how this could be fixed?

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    in next(), set pid to 0 instead of 1 so that when you +1 it wont be 2, it will be 1, which is what you want,
    and in previous(), set it to 7 to get at 6 after the -1...
    davi likes this.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Or you can do it this way to avoid having an extra step to reset pid.
    Code:
    $projects.data('id', (pid == 6) ? 1 : (pid + 1));
    ...
    $projects.data('id', (pid == 1) ? 6 : (pid - 1));
    davi likes this.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New Coder
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Or you can do it this way to avoid having an extra step to reset pid.
    Code:
    $projects.data('id', (pid == 6) ? 1 : (pid + 1));
    ...
    $projects.data('id', (pid == 1) ? 6 : (pid - 1));
    Perfect! Thanks (:

    Cheers for the help aswell rnd.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by glenngv View Post
    Or you can do it this way to avoid having an extra step to reset pid.
    Code:
    $projects.data('id', (pid == 6) ? 1 : (pid + 1));
    ...
    $projects.data('id', (pid == 1) ? 6 : (pid - 1));
    another option uses modulus:
    Code:
    // both forward and reverse
    $projects.data('id', (pid + 6) % 6 + 1);
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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