是否可以将setTimeout()
用于选择器?例如:
$('.some-class').someFunction({
option1: 500,
option2: 'blue'
});
该功能由点击.some-class
的jquery插件触发。我希望有一秒延迟。
答案 0 :(得分:0)
我认为你所追求的是这样的:JSFiddle
创建新功能:
//Create new jQuery function "someFunction"
$.fn.someFunction = function(options) {
//Set defaults
var settings = $.extend({
color: "red",
time: 1000
}, options); //Use options when supplied
return this.each(function() {
$(this).delay(settings.time).queue(function (next) {
$(this).css("color", settings.color);
next();
});
});
};
致电我们的新功能:
//Change some-class to blue after 1500 milliseconds
$('.some-class').someFunction({
time: 1500,
color: 'blue'
});
答案 1 :(得分:0)
您可以修改插件.somefunction
以支持:
// store the original plugin:
var orig_f = $.fn.somefunction;
// redefine it to accept a delay property
$.fn.somefunction = function (obj) {
if (obj.delay === undefined) {
// standard behaviour
return orig_f.apply(this, arguments);
}
// delay the call
setTimeout(orig_f.bind(this, ...arguments), obj.delay);
// or if the ES6 spread operator cannot be used:
// setTimeout(Function.apply.bind(orig_f, this, arguments), obj.delay);
return this;
}
示例电话:
$('.some-class').someFunction({
option1: 500,
option2: 'blue',
delay: 1000
});