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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Dofunction before getting variable value.

    I am creating a slider and I want the buttons to dissapear when it gets to the ends. They currently do that but if I quickly click a button multiple times the slider will keep going.

    The problem is the variables are getting their values before the div moves. I want the move_animate function to completely finish before getting the values. Any help would be greatly appreciated

    function leftarrow(){

    move_animator('+=700px');

    var mydiv=document.getElementById("mover")
    var marginleft = getStyle(mydiv, 'marginLeft')

    if(marginleft === "-2100px"){
    showstuff('right_arrow');
    }
    if (marginleft === "-700px"){
    hidestuff('left_arrow');
    }

    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,938
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    This is a *GUESS*, since you don't show us the code for move_animator, but I think it's likely a good one:

    That's because the move_animator has to do its work in pieces, using setTimeout() or setInterval. So it *STARTS* the move but then immediately returns (if it didn't, JavaScript would be locked up until it finished).

    There are probably several ways to fix this, but the easiest would be to modify the code in move_animator to not accept another command until the current one is finished. In any case, you'll surely have to dig into its code.
    Last edited by Old Pedant; 10-04-2011 at 07:18 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks that makes sense . Here is the code for the move function. I have no idea how to make it do that .

    function move_animator(px) {
    $('#mover').animate({
    'marginLeft' : px
    });
    }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,938
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Ahhh...neither do I. You never said you were using jQuery. No way you want to dig into the guts of the jQuery library to change something like that!

    You may have to approach this differently.

    How many pictures are there, total? And do you start out showing the leftmost or the rightmost or some other one?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    lol i'm new to this forum stuff. I should have told you about that code before.

    I want the slider to display content if that makes a difference. I'm not sure how many slides I want yet, it might end up being about 20. And I want it to start from the left.

    So if you have any ideas or a link to some code please let me know

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,938
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Well, my idea is a simple one: COUNT the clicks. Keep track of where the slider is/should be and make the arrows disappear based on the count.
    Code:
    var NUMBER_OF_SLIDES = 20; // or however many you have
    var maxslide = NUMBER_OF_SLIDES - 1;
    
    var currentSlide = 0; // left most slide
    
    function rightarrow()
    {
        if ( currentSlide == maxslide ) return; // ignore if already at far right
        ++currentSlide;
        if ( currentSlide >= maxslide )  /* should never be >, but can be == */
        {
            hidestuff("right arrow");
        }
        showstuff("left_arrow"); // and if we aren't at zero, then left arrow always shown
        move_animator('-=700px');
    }
    function leftarrow()
    {
        if ( currentSlide == 0 ) return; // ignore if already at far left
        --currentSlide;
        if ( currentSlide <= 0 ) /* should never be < 0, but can be == */
        {
            hidestuff("left arrow");
        }
        showstuff("right_arrow"); // and if we aren't at max, then right arrow always shown
        move_animator('+=700px');
    }
    That seems logical, at least.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    laserchalk (10-05-2011)

  • #7
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'll give it a go

  • #8
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    YOU ARE A GENIUS! thank you so much for helping me. So are the variables that aren't inside the functions global and when you declare variables inside functions they are local?

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,938
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Yes and yes.

    On the nose.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I had problems with the divs not hiding but I ended up fixing the problem.

    Code:
    var NUMBER_OF_SLIDES = 4; // or however many you have
    var maxslide = NUMBER_OF_SLIDES - 2;
    
    var currentSlide = 0; // left most slide
    
    function rightarrow()
    {
        if ( currentSlide == maxslide ) {
    		hidestuff('right_arrow');
    		} 
        if ( currentSlide >= 0 ) {
    		showstuff('left_arrow');
    		}
    	++currentSlide;
        move_animator('-=320px');
    }
    function leftarrow()
    {
        if ( currentSlide == 1 ){ 
    	hidestuff('left_arrow');
    	} 
        if ( currentSlide <= NUMBER_OF_SLIDES ) {
    		showstuff('right_arrow');
    		}
    	--currentSlide;
        move_animator('+=320px');
    }
    Well I do believe I have learnt how to make a counter


  •  

    Posting Permissions

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