gpt4 book ai didi

javascript - 暂时停止函数jquery

转载 作者:行者123 更新时间:2023-12-01 04:01:01 25 4
gpt4 key购买 nike

var start = $('#start_img');

start.on('click', function(){
var piano = $('.piano');
piano.each(function(index){
$(this).hide().delay(700 * index).fadeIn(700*index);
start.off('click');
})

});

您可以看到我使用了 start.off('click') 方法来阻止事件监听器在调用后再次运行。但问题是,我只希望事件监听器在事件运行期间关闭。这样当事件仍在运行时就无法再次调用它。但是一旦事件结束,我希望它能够再次“可调用”。有谁知道如何做到这一点?

其他方法可以做到这一点(也不起作用)。有人能帮我一下吗。另一个现在已经清楚了。

var start = $('#start_img');



start.on('click', function() {
var q = 0;
var piano = $('.piano');
if (q === 1) {
return; // don't do animations
}
else{
piano.each(function(index) {
q = 1;
$(this).hide()
.delay(700 * index)
.fadeIn(700 * index, function() {
// remove from each instance when animation completes
q = 0
});

});}

});

最佳答案

您也可以在事件元素上切换类,然后您可以检查该类,如果存在则不执行任何操作

start.on('click', function() {
var piano = $('.piano');
if (piano.hasClass('active')) {
return; // don't do animations
}
piano.each(function(index) {
$(this).addClass('active')
.hide()
.delay(700 * index)
.fadeIn(700 * index, function() {
// remove from each instance when animation completes
$(this).removeClass('active')
});

});

});

关于javascript - 暂时停止函数jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42191148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com