JS:
$.fn.blink = function(times, duration) {
times = times || 2;
while (times--) {
this.fadeTo(duration, 0).fadeTo(duration, 1);
}
};
这会使文字闪烁。最后眨眼我想执行一个函数,所以我改变了它:
$.fn.blink = function(times, duration, callback) {
times = times || 2;
while (times--) {
this.fadeTo(duration, 0).fadeTo(duration, 1);
}
if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
}
};
回调的执行不在正确的位置。当眨眼开始时似乎被解雇了。有关如何在最后一次闪烁时调用该功能的想法吗?
答案 0 :(得分:1)
jQuery动画有一个complete
回调参数,因为这些方法是异步的
尝试:
while (times--) {
this.fadeTo(duration, 0).fadeTo(duration, 1, function(){
/*last animation is complete*/
if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
}
});
}
答案 1 :(得分:1)
$(document).ready(function () {
$('.blink').blink(5, 500, shout);
});
function shout() {
alert('finished blinking');
}
$.fn.blinkOnce = function (duration, callback) {
this.fadeTo(duration, 0).fadeTo(duration, 1, function () {
if (typeof callback == 'function') callback.call(this);
});
};
$.fn.blink = function (times, duration, callback) {
var toBlink = this;
var blinkComplete = function () {
times--;
if (times == 0) callback.call(this);
else toBlink.blinkOnce(duration, blinkComplete);
}
toBlink.blinkOnce(duration, blinkComplete);
return true;
};