0

I have a custom jQuery function that gets an input options object, and a callback function to run after it's completed. Also, inside the jQuery function, I have created a callback object that contains functions which I'd like to be available in the callback function.

keyboardInput : function(input, callback){
    // ...
    if(typeof callback === 'function') {
        callback.call(self.callback);
    }
    // ...
    self.callback = {
        hideCursor : function(){self.hiddenCursor = true;},
        showCursor : function(){self.hiddenCursor = false;},
    }
}

So I call my jQuery function, and the callback runs, but I do not have the self.callback object inside.

$('.txt-fx.keyboard-input.first').keyboardInput({
    speed : [60, 120],
    cursor : 'box',
    cursorBorderWidth : 3,
    delay : 2000,
}, function(obj){
    setTimeout(function(){
        obj.hideCursor();
        $('.txt-fx.keyboard-input.second').keyboardInput({
            speed : [60, 120],
            cursor : 'box',
            cursorBorderWidth : 3,
           delay : 2000,
        });
    }, 3000)
});

throws:

Uncaught TypeError: Cannot read property 'hideCursor' of undefined

What am I doing wrong?

1 Answer 1

1

syntax for .call is fn.call(thisarg, parameter1, parameter1 ...)

you are setting the this for your callback as self.callback - see MDN Documentation of function.call

you probably want to do:

callback.call(null, self.callback);
Sign up to request clarification or add additional context in comments.

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.