views:

41

answers:

1

Updated Question:

JQuery animation repeats over and over if it encounters an error. Having a return or return false doesn't stop the animation from repeating. What would be the best practice to fix this error? Thanks.

<div id="label">
    FADE OUT
</div>

<script type="text/javascript">
     $(function() {

        $("#label")
            .animate(
                {opacity: 0}, 
                1000,
                function()
                {
                    throw new Error("Some error!");
                }
            );

     });
</script>

Original Question:

Not sure why this is but after using jQuery for sometime (actually before it became mainstream) I still don't understand why certain functions will keep repeating when it encounters an error.

Example: - Make an ajax call and if the callback encounters an error the ajax call will keep calling the callback. This results in hundreds of errors in the firebug console.

  • Even if you use .trigger(event) if the event you trigger has code that encounters an error jQuery keeps triggering the event over & over.

*I've tried using a try-catch and having a return, or even return false ... but it still doesn't stop jQuery from repeating the event over and over.

My question is 1.) How to stop it? and 2.) I'm very curious as to why this is happening if any one knows.

Thanks!

+1  A: 

What do you mean repeating 'over and over'? If I make a handler like so:

$('a').click( function() { throw 'oops'; } );

... are you expecting that on the first click you get an error and then after that it stops calling the handler ...?

thenduks
yes exactly... you should get only one 'oops' in the error console. Not 'oops' over and over.
tony
I encounter this more with ajax callbacks.
tony
You will get it only once per click, in my example code. Is that not what you expect? What happens with ajax callbacks? Everytime you make the call it will attempt to call the callback... is that not expected?
thenduks
Hey thanks for all the feedback. In an attempt to give an example as @thenduks recommended I used the firebug tool to trace the stack. That's when I realized it wasn't so much the ajax call that was causing the error. I have an animation and the callback of that animation makes the ajax call. So I realized the problem was the animation callback. I should have realized that especially when I noticed it kept getting called over and over again.
tony
@tony Could you edit your original post with this new information so people coming here will know how it resolved? Or better yet...answer the question and mark your own answer as the correct one.
treeface
@treeface thanks I updated the question, I'm still hoping someone can help me solve it. thanks.
tony