Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6668

jQuery - TypeError: callback is undefined

I made one small jQuery plugin for discount and after discounting if I don't use callback function I recive error: "TypeError: callback is undefined"

CODE:

$.fn.countdown = function (options,callback) {
    var settings=$.extend({
        duration        :   0,
        interval        :   1000,
        text_before     :   "",
        text_after      :   "",
        disable         :   false,
    }, options );
    var callback,
        container = $(this[0]).html(settings.text_before + settings.duration + settings.text_after),
        countdown = setInterval(function (){
        var dd = --settings.duration;
        if(settings.disable==true){
            clearInterval(countdown);
        }
        else if (dd > 0){
            container.html(settings.text_before + dd + settings.text_after);
        } else {
            clearInterval(countdown);
            callback.call(container);   
        };
    }, settings.interval);
};

EXAMPLE 1 - Using Callback function:

$("#timer").countdown({
            duration    :   3,              // Discount start from defined number (in this case 3 seconds) DEFAULT: 0
            text_before :   "Redirection begins in exactly ",   // initial text before number (example: Redirection begins in exactly 3), DEFAULT: blank
            text_after  :   " seconds"  // initial text after number (example: 3 seconds), DEFAULT: blank
        },
        // callback function when discount stop on 0
        function(){
            this.html("Done counting, redirecting.");
            window.location = "http://www.creativform.com";
        });

EXAMPLE 2 - Without callback function:

$("#timer").countdown({
            duration    :   3,              // Discount start from defined number (in this case 3 seconds) DEFAULT: 0
            text_before :   "Redirection begins in exactly ",   // initial text before number (example: Redirection begins in exactly 3), DEFAULT: blank
            text_after  :   " seconds"  // initial text after number (example: 3 seconds), DEFAULT: blank
        });

Any help?

Upvotes: 0

Views: 1093

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

Try this

$.fn.countdown = function (options,callback) {
    var settings=$.extend({
        duration        :   0,
        interval        :   1000,
        text_before     :   "",
        text_after      :   "",
        disable         :   false,
    }, options );
    var 
        container = $(this[0]).html(settings.text_before + settings.duration + settings.text_after),
        countdown = setInterval(function (){
        var dd = --settings.duration;
        if(settings.disable==true){
            clearInterval(countdown);
        }
        else if (dd > 0){
            container.html(settings.text_before + dd + settings.text_after);
        } else {
            clearInterval(countdown);

            if (typeof callback === 'function') {
                callback.call(container);   
            }
        };
    }, settings.interval);
};

Upvotes: 3

Related Questions