创建您的 jquery 函数:
(function ($) {
function getTimer(obj) {
return obj.data('swd_timer');
}
function setTimer(obj, timer) {
obj.data('swd_timer', timer);
}
$.fn.showWithDelay = function (delay) {
var self = this;
if (getTimer(this)) {
window.clearTimeout(getTimer(this)); // prevents duplicate timers
}
setTimer(this, window.setTimeout(function () {
setTimer(self, false);
$(self).show();
}, delay));
};
$.fn.hideWithDelay = function () {
if (getTimer(this)) {
window.clearTimeout(getTimer(this));
setTimer(this, false);
}
$(this).hide();
}
})(jQuery);
用法:
$('#spinner').showWithDelay(100); // fire it when loading. spinner will pop up in 100 ms
$('#spinner').hideWithDelay(); // fire it when loading is finished
// if executed in less than 100ms spinner won't pop up
演示: