我在循环中有这个功能:
function blink(selector){
$(selector).fadeOut(800, 'easeInQuint', function(){
$(this).fadeIn(800, function(){
blink(this);
});
});
}
我可以用:
来调用它$('#someId').on('click', function() {
blink('#someId2');
});
但我怎么能阻止它?
$('#someOtherId').on('click', function() {
// stop blink function
});
答案 0 :(得分:1)
添加停止条件以闪烁:
function blink(selector,stop){
if (stop) {
$(selector).stop(true,true).show();
return;
}
$(selector).fadeOut(800, 'easeInQuint', function(){
$(this).fadeIn(800, function(){
blink(this);
});
});
}
$('#someOtherId').on('click', function() {
blink(theidtostop, true)
// stop blink function
});
您也可以使用适当的方法将blink
转换为模块。
var blink = {
blinkingElements: [],
doBlink: function () {
$(this.blinkingelements)
.fadeIn( 800, 'easeInQuint' )
.fadeOut( 800, $.proxy( this.doBlink, this ) );
},
startBlink: function ( el ) {
this.blinkingElements = blinkingElements.concat( $.makeArray(el) );
this.doBlink();
},
stopBlink: function () {
$(this.blinkingElements).stop(true,true).show();
}
clearBlinkingElements: function () {
this.stopBlink();
this.blinkingElements = [];
}
}
$('#someId').on('click', function() {
blink.clearBlinkingElements();
blink.startBlink($('#someId2'));
});
$('#someOtherId').on('click', function() {
blink.clearBlinkingElements();
blink.startBlink($('#someOtherId2'));
});
或只是
$('.somecommonclass').on('click', function() {
blink.clearBlinkingElements();
blink.startBlink($('#' + this.id + '2'));
});
答案 1 :(得分:0)
$('#someOtherId').on('click', function() {
// stop blink function
$('#someId2').stop(true, true)
});