SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help Passing Function Argument Value to Another Function

    What syntax do I use to pass the div_name argument value to the rotate function in the setInterval function? I can't seem to get it to work.

    Code JavaScript:
    function theRotator(div_name) {
        //Set the opacity of all images to 0
        $('div.'+div_name+' ul li').css({opacity: 0.0});
     
        //Get the first image and display it (gets set to full opacity)
        $('div.'+div_name+' ul li:first').css({opacity: 1.0});
     
        //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
     
        setInterval('rotate('+div_name+')',6000);
     
    }

    Thanks

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use an anonymous function:

    Code javascript:
    setInterval(function() {
      rotate(div_name);
    }, 6000);

    The problem with what you're doing is that you have to pass a function reference to setInterval. In what you posted, setInterval is getting the result of the rotate function (i.e. what it returns) - which could be nothing at all. It would work if you did this:

    Code javascript:
    setInterval(rotate, 6000);
    but since you want to pass it an argument, you need to wrap it in an anonymous function. Then setInterval will execute that function every 6s.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Raffles, That worked! Thank you so much!


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
  •