SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question setTimeout issue

    Hi there,

    So I'm writing an image slideshow in jQuery, and I have an array of img srcs which I am sending to a function:

    Code:
    function revolve(array, delay){
    	var src = array[0];
    	var nextSrc = array[1];
    	$('#window img[src="' + nextSrc + '"]').css({"z-index": 50});
    	$('#window img[src="' + src +'"]').css({"z-index": 100}).fadeOut(1000, function(){
    		$(this).css({"z-index": 0}).show();
    	});
    	array.shift();
    	array.push(src);
    	timer = setTimeout("revolve(" + array + ", " + delay + ")", delay + 1000);
    }
    now, when the script gets down to the last line, I get this error:
    Uncaught SyntaxError: Unexpected token ILLEGAL

    But when I call the function like so:
    Code:
    revolve(images, 1000);
    It runs fine. I only get the error message when I use a setTimeout();

    I'm not sure what's happening because the error message doesn't make a lot of sense to me, plus I couldn't find anything that seemed particularly relevant online about it.

    Thanks in advance,
    Mike

  2. #2
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does:

    var timer = setTimeout("revolve(" + array + ", " + delay + ")", delay + 1000);

    do the trick?
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  3. #3
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi beebs93, thanks for your reply.

    I actually set var timer; before I called the function. I realise it has something to do with the array, because the setTimeout takes a string, so converts the array into a string before it passes it to the function. The function is looking for an array, and so gives the error.

    If I change the function like so:
    Code:
    function revolve (delay){
    var src = images[0];
    var nextSrc = images[1];
    
    // etc etc
    
    images.shift();
    images.push(src);
    timer = setTimeout("revolve(" + delay + ")", 1000 + delay);
    It works fine. That'll do the trick for the moment, but I'd love to know how to pass an array to a function using setTimeout.

    Anyway, thanks for your input,
    Mike


Tags for this Thread

Bookmarks

Posting Permissions

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