admin管理员组文章数量:1323225
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 ;)
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 ;)
Share Improve this question asked Jul 24, 2012 at 6:36 Alistair LeeAlistair Lee 811 silver badge9 bronze badges2 Answers
Reset to default 4Within your function, do something like this:
jQuery.somefunctionname = function (options) {
if (typeof options.onStart === "function")
options.onStart.call(this);
// other function code here
if (typeof options.onComplete === "function")
options.onComplete();
// OR
options.onComplete.call(this);
// OR
options.onComplete.apply(this, argsArrayIfDesired);
// etc.
};
That is, if the property is defined in options
and actually is a function then call it at the appropriate point. Specify a setting for this
using .call()
or .apply()
if desired, and include any parameters. Integrate with your settings
object or not as desired.
Simply have a default value for those settings of jQuery.noop
, and then you can know that it is safe to call the value of that setting as a function, using apply()
or similar.
e.g.
jQuery.somefunctionname = function (options) {
var settings = {},
defaults = {
'someoption': 'somevalue',
'someoption2': 'somevalue2',
'someoption3': 'somevalue3',
'onStart': jQuery.noop,
'onComplete': jQuery.noop
}
settings = $.extend({}, defaults, options);
settings.onStart.apply(this);
//do something functional
alert("hey i'm a function");
settings.onComplete.apply(this);
}
本文标签: javascripthow to create jquery function using onstart and oncompleteStack Overflow
版权声明:本文标题:javascript - how to create jquery function using onstart and oncomplete - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742082123a2419761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论