CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2011
    Posts
    7

    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

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    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.

  3. #3
    Join Date
    Mar 2011
    Posts
    7

    The Problem persist.

    Quote Originally Posted by PeejAvery View Post
    That's why setInterval() exists...
    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.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    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.

  5. #5
    Join Date
    Mar 2011
    Posts
    7

    Re: Help, setTimeout not working properly.

    ok, got it, but why the + signs ?

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Help, setTimeout not working properly.

    Ever concatenated a string?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Mar 2011
    Posts
    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

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    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
  •  





Click Here to Expand Forum to Full Width

Featured