|
-
March 15th, 2011, 12:59 AM
#1
Help, setTimeout not working properly.
Hey,
I have been trying to create an infinite loop using "setTimeout" in javascript to animate a slideshow.
Following is the code I am using :
Code:
$(document).ready(function()
{
var currentPosition = 1;
var slideWidth = 640;
var slides = $('.slide');
var numberOfSlides = slides.length;
// Wrap all .slides with #slideInner div
slides.wrapAll('<div id="slideInner"></div>')
// Set #slideInner width equal to total width of all slides
$('#slideInner').css('width', slideWidth * numberOfSlides);
slidetheslider(currentPosition,numberOfSlides);
});
function slidetheslider(pos,numSlide)
{
var curPos = pos ;
var numberSlide = numSlide ;
$('#slideInner').animate({
'marginLeft' : 640*(-curPos)
},1500 );
if (curPos >= numberSlide-1)
{
curPos = 0;
}
else
{
curPos = curPos + 1;
}
window.setTimeout("slidetheslider(curPos,numberSlide)",3000);
};
The setTimeout works as it is supposed to if I do something like ( window.setTimeout("slidetheslider(6,6)",3000); ) but doesn't in the above code.
It is already 48hours of headache and I couldn't find a clue.Any help ?
Thanks a lot
Lisa
-
March 15th, 2011, 08:04 AM
#2
Re: Help, setTimeout not working properly.
That's why setInterval() exists...
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 15th, 2011, 09:17 AM
#3
The Problem persist.
 Originally Posted by PeejAvery
Hey thanks, but the problem seems to remain.
same as in above, if I pass numerical value instead of a variable it works, must be something wrong in the way I am passing the values to the function ?
the first call execute as it should be, here( slidetheslider(currentPosition,numberOfSlides);
) but the function isn't called when called from setInterval or setTimeout.
-
March 15th, 2011, 10:02 AM
#4
Re: Help, setTimeout not working properly.
That's because you have them enclosed in quotes which means they are acting as part of a larger string. The following should do it...
Code:
"slidetheslider(" + curPos + ", " + numberSlide + ")"
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 15th, 2011, 10:08 AM
#5
Re: Help, setTimeout not working properly.
ok, got it, but why the + signs ?
-
March 15th, 2011, 10:09 AM
#6
Re: Help, setTimeout not working properly.
Ever concatenated a string?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 15th, 2011, 10:17 AM
#7
Re: Help, setTimeout not working properly.
yup, **** it is so different then c++ where integers are integers and strings are strings and you can pass a pointer to a function.
Still didn't get, why strings ? But anyway thanks for the help
-
March 15th, 2011, 11:30 AM
#8
Re: Help, setTimeout not working properly.
The function name and parameters need to be passed as a string. That's what the window object function demands.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|