Search code examples
javascriptjquerycallbackcycle

jQuery Cycle Plugin callback error


I can't get before & after callbacks to work with the cycle plugin for jQuery!

I'm not sure what's going wrong, i even tried using the sample code from the documentation.

Here's the code:

$(document).ready(function(){

    function onBefore() { alert('before'); }

    $('.slideshow').cycle({
        before: 'onBefore'
    });
});

and it throws an error: "Error: opts.before[0].apply is not a function"

and in chrome: "Uncaught TypeError: Object onBefore has no method 'apply'"

what is happening!?


Solution

  • The error is because .apply() is a method on functions, not on strings...and 'onBefore' is a string. Instead, don't use a string...use a direct reference, like this:

    $(document).ready(function(){    
        function onBefore() { alert('before'); }    
        $('.slideshow').cycle({
            before: onBefore
        });
    });
    

    Or an anonymous function:

    $(function(){
        $('.slideshow').cycle({
            before: function() { alert('before'); }
        });
    });