In my game, I have a grid filled with words. To pronounce the words that the user must click on the letters on the side called "drag". When a letter is clicked, it enlivens the position in the grid.
The problem I am facing is that the user can quickly press letters, which leads to a program crash. To solve this problem, I want to disable click events until the animation completes.
In the past, I used the setTimeOut function before, but this is not a reliable method, as it always depends on the speed of the browser.
Here is the click event:
$('.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++; } });
source share