-1

check the below code

var fun = function() {
   $('.recode-list').animate({top:-heigth},quantity*1500,'linear',function() {
     console.log(count++);

     var temp = $('.recode-list .item:lt('+quantity+')');
     var activity = temp.clone();

     temp.remove();
     $('.recode-list').append(activity);
     $('.recode-list').css('top',0);
     fun();
   });
}
fun();

In that situation the fun's call stack would be overflow in soon or later, I need implement the feature what the above code does, a animation loop How to fixed it?

sinbar
  • 933
  • 2
  • 7
  • 25
  • 1
    `fun();` will cause never-ending loop. Instead, put a condition. If you explain what you are trying to achieve, we might be able to help you – Rajesh Nov 29 '17 at 04:57
  • @Rajesh the point is, I need implement the feature what the above code does, a animate loop. – sinbar Nov 29 '17 at 04:59
  • This is a poor design. Have a look at [this](https://stackoverflow.com/questions/399867/custom-events-in-jquery) SO question for using events. – Jeroen Heier Nov 29 '17 at 05:00
  • @Rajesh I want a animation loop with the custom function which I can do some reset. – sinbar Nov 29 '17 at 05:03
  • @sinbar My point was, there are certain animations that you can achieve using CSS. Using CSS in such case is always better. So if you can explain the animation part, like *I want to toggle visibility every second, or rotate on hover*, it would be easy for us to suggest you alternatives. Just saying, *I need implement the feature what the above code does* is no explanation. – Rajesh Nov 29 '17 at 05:07

1 Answers1

0

Make the break point that the function can be ended. Such as

if(count === 10){
  return;
}

Otherwise, it won't end forever.

moon
  • 640
  • 6
  • 14
  • Maybe, maybe not, you should set the 'point' that means when you want the animation would be stopped. – moon Nov 29 '17 at 05:03
  • @Rajesh As you commented, this person asking this question should let us know what he/she wants to do with his/her code. If we/I don't know, I don't have any options but saying 'You need to make the certain point to finish the recursive function, since the recursion is the reason to make stack overflow error' – moon Nov 29 '17 at 05:08
  • Since you have gone through the conversation, my suggestion is *not to answer*. Ask question in comments for clarity. That way, when you answer, it will be a concrete answer and it will not attract unwanted comments like these – Rajesh Nov 29 '17 at 05:10
  • @Rajesh Oh, Yeah, I agree with you of that point. I should've also thought one more before answering. Thanks for your advice – moon Nov 29 '17 at 05:13