在我的游戏中,我有一个用单词填充的网格。为了拼写单词,用户必须点击侧边栏上称为“拖动”的字母。当点击字母时,它会动画地移动到网格上的位置。
问题在于用户可以快速点击字母,导致程序崩溃。为解决此问题,我想禁用点击事件,直到动画完成。
过去我曾使用
以下是点击事件:
问题在于用户可以快速点击字母,导致程序崩溃。为解决此问题,我想禁用点击事件,直到动画完成。
过去我曾使用
setTimeOut
函数,但这不是一种可靠的方法,因为时间取决于浏览器速度。以下是点击事件:
$('.drag').on('click', function (e) {
e.preventDefault();
var target = $('.highlight-problem .drop-box:not(.occupied):first');
var targetPos = target.parents('td').position();
console.log(targetPos);
var currentPos = $(this).offset();
var b = $(this);
if (target.length) {
target.addClass("occupied");
$(".occupied").parent(".flip-wrapper").addClass("flipped");
var clonedObject = b.clone()
if (b.data("letter") == target.parents('td').data("letter")) {
clonedObject.addClass("right-letter");
target.addClass('right-letter');
setTimeout(function () {
hitSound.play();
}, 550);
} else {
clonedObject.addClass("wrong-letter");
target.addClass('wrong-letter');
setTimeout(function () {
missSound.play();
}, 550);
}
clonedObject.appendTo("table").css({
position: "absolute",
top: currentPos.top,
left: currentPos.left
}).stop().animate({
top: targetPos.top,
left: targetPos.left
}, "slow", function () {
$(this).prop('disabled', false).css({
top: 0,
left: 0
}).appendTo(target);
var spellWord = $('.highlight-problem .drop-box');
if (!spellWord.filter(':not(.occupied)').length) {
var wordIsCorrect = 0;
spellWord.each(function () {
if ($(this).parents('td').data("letter") == $(this).find("div").data("letter")) {
console.log('letter is: ' + $(this).find("div").data("letter"))
wordIsCorrect++;
}
});
.is(':animated')
或者$(':animated').length
可以帮助你!:)
- Tats_innit