...

View Full Version : Dofunction before getting variable value.



laserchalk
10-04-2011, 05:56 AM
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 :D

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');
}

}

Old Pedant
10-04-2011, 07:12 AM
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.

laserchalk
10-04-2011, 11:04 PM
Thanks that makes sense :thumbsup: . 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
});
}

Old Pedant
10-05-2011, 12:32 AM
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?

laserchalk
10-05-2011, 03:58 AM
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 :)

Old Pedant
10-05-2011, 04:22 AM
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.


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.

laserchalk
10-05-2011, 05:23 AM
I'll give it a go :)

laserchalk
10-05-2011, 05:29 AM
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?

Old Pedant
10-05-2011, 05:41 AM
Yes and yes.

On the nose.

laserchalk
10-10-2011, 01:41 AM
I had problems with the divs not hiding but I ended up fixing the problem.


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 :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum