0

i m having a jquery animate function to animate a list which is specified using

    $li = $("ol#update > li");

the animate function

function animate_li()
{
   $li.filter(':first')
      .animate({
         height:  'show',
         opacity: 'show'
      }, 250, function(){
        animate_li();
      });
    $li = $li.not(':first');
}

now i want to parameterize the animate_li function which i have done as:

function sp_animate_li(sp)
{
    console.log ( $li );
    function animate_li()
    {
        $li.filter(':first')
        .animate({
         height:  'show',
         opacity: 'show'
        }, sp, function(){
            animate_li();
        });
        $li = $li.not(':first');
    }
}

but now i cannot get any animation using the call

sp_animate_li(100);

Any help

thanks

4 Answers 4

1

You are not calling the inner function, nor returning it, so you can't actually call it. Try this:

function sp_animate_li(sp)
{
    console.log ( $li );
    function animate_li()
    {
        $li.filter(':first')
        .animate({
         height:  'show',
         opacity: 'show'
        }, sp, function(){
            animate_li();
        });
        $li = $li.not(':first');
    }
    return animate_li;
}

var inner = sp_animate_li(100);
inner(); // calls the inner function
Sign up to request clarification or add additional context in comments.

1 Comment

oops... java and javascript are different... javascript can map variable to function... wow!!!
1

Why not define your function as:

function animate_li($li, sp)
{
   sp = sp || 250;
   $li.first()
      .animate({
            height:  'show',
            opacity: 'show'
          }, 
          sp, 
          function(){
              animate_li($li.not(':first'), sp);
          }
      );
}

This also avoids the use of variables from higher scopes which can get confusing...

Update: Fixed recursive call.

2 Comments

this wont work as animate_li is a recursive function(being called by itself in line 9) otherwise it would work with original with only sp parameter... animate_li(sp)
@Pradyut Bhattacharya: You should include your version in your question (edit your question) instead of posting it as an answer (if it is not an answer) and explain why it might not work. And sorry, I missed the recursive part, but it is easy to fix (what I did).
0

No wonder, cause animate_li isn't called anywhere. Why wouldn't you add parameter to animate_li instead of wrapping it?

Comments

0
function animate_li( sp)
{
   sp = sp || 250;
   $li.filter(':first')
      .animate({
         height:  'show',
         opacity: 'show'
      }, sp, function(){
        animate_li( sp);
      });
    $li = $li.not(':first');
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.