I’m trying to create my own jquery function, I know how to add options but don’t know how to add ‘onStart’ or ‘onComplete’ functionality.
this is what I know so far:
jQuery.somefunctionname = function (options) {
var settings = {},
defaults = {
'someoption': 'somevalue',
'someoption2': 'somevalue2',
'someoption3': 'somevalue3'
}
settings = $.extend({}, defaults, options);
//do something functional
alert("hey i'm a function");
}
But If I want the user to be able to add their own code in before (onStart) and after (onComplete) my function, what should I code?
User should be able to write like this:
$.somefunctionname({
'someoption' : 'a',
'someoption2' : 'b',
'someoption3' : 'c',
'onStart' : function() {
//whatever user want when my function started
},
'onComplete' : function() {
//whatever user want when my function ended
} });
thx 😉
Within your function, do something like this:
That is, if the property is defined in
optionsand actually is a function then call it at the appropriate point. Specify a setting forthisusing.call()or.apply()if desired, and include any parameters. Integrate with yoursettingsobject or not as desired.