我在谷歌上搜索并遇到了许多类似的问题,人们试图在 jQuery 插件中设置超时但我正在努力寻找答案,这不是一个懒惰的帖子。
我试图在调用动画以隐藏某些内容时实现延迟,例如,如果用户将鼠标悬停在某个区域上,则会有更多内容进入视图并隐藏原始内容。然后,当用户在 2 秒后将鼠标移开时,将返回原始内容。
动画按预期工作,尽管忽略了超时。这是我无法理解的!
对代码的任何帮助将一如既往地不胜感激!
这是简化的代码,专注于动画,但我保留了插件结构:-
;(function($){
$.fn.extend({
pluginName: function(options) {
// - Settings list and the default values
var defaults = {
width: this.css('width'),
};
var options = $.extend({}, defaults, options);
return this.each(function() {
// -- Globals
var o = options;
var timeoutID;
function deceptionAnimate(display) {
if(display == 1) {
obj.clearQueue().animate({
'top': 0,
'left': -o.width
}, o.interval, o.easing);
} else if(display == 0) {
obj.clearQueue().animate({
'top': 0,
'left': 0
}, o.interval, o.easing)
}
}
function delaydeceptionAnimate () {
timeoutID = window.setTimeout(deceptionAnimate(0), 2000);
}
// ---- Initiate
function init() {
// ----- Animate
$(document).on(o.eventTrigger, wrapperID, function() {
deceptionAnimate(1);
});
$(document).on('mouseout', wrapperID, function() {
delaydeceptionAnimate(0);
});
}
// Call
init();
});
}
});
})(jQuery);