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
Re: Help, setTimeout not working properly.
That's why setInterval() exists...
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 + ")"
Re: Help, setTimeout not working properly.
ok, got it, but why the + signs ?
Re: Help, setTimeout not working properly.
Ever concatenated a string?
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
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.