Control click events using a counter and add stylish transitions to both disable and re-enable them

Iā€™m looking for some assistance with the code snippets on my webpage - check it out here.

HTML:

<button class="wrong-answer" onclick="showResult(this)">42</button>
<button class="right-answer" onclick="showResult(this)">43</button>

<p id="answer" class="answer-display-hidden">answer</p>

<div class="incorrect">
    <span>Incorrect:</span>
    <p>0</p>
</div>

<div class="correct">
    <span>Correct:</span>
    <p>0</p>
</div>

CSS:

.answer-display-visible {
  visibility: visible;
  opacity: 1;
  transition: opacity 1s linear;
}

.answer-display-hidden {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 1s, opacity 1s linear;
}

.incorrect, .correct {float: left; padding-right: 20px}

JS:

var incorrectCalls = 0;
var correctCalls = 0;

function showResult(b) {
  var res = document.getElementById('answer');

  if (b.classList.contains('right-answer')) {
    res.innerHTML = '<span class="right">right</span>';

    correctCalls++;
        var cor = $('.correct > p:first');
        cor[0].innerHTML = correctCalls;
  } 

  else {
    res.innerHTML = '<span class="wrong">wrong</span>';

    incorrectCalls++;
    var incor = $('.incorrect > p:first');
    incor[0].innerHTML = incorrectCalls;
  }

  res.classList.remove("answer-display-hidden");
  res.classList.add("answer-display-visible");

  setTimeout(function() {
        res.classList.add("answer-display-hidden");
  }, 2000);
}

I need help figuring out how to temporarily pause the activation of the `right-answer` counter while the text is fading in and out. I want to prevent users from manipulating the counter by quickly clicking the button before the text appears.

Answer ā„–1

One way to handle button click events is by using the setTimeout function.

Check out the Live Demo here

Here's a snippet of the code:

function processButtonClick(btn) {
 .
 .
 .
  $(".btn-success").prop("disabled", true);
  $(".btn-danger").prop("disabled", true);
  setTimeout(function() {
    $(".btn-success").prop("disabled", false);
    $(".btn-danger").prop("disabled", false);
  }, 3000);
}

Answer ā„–2

Here is my solution

Click here for the live demonstration

I have removed the CSS code and below is the jQuery code snippet:

$(document).ready(function(){

    $(".wrong-answer").click(function(){
        $(".right-answer").attr("disabled","disabled");
        $("#answer").text("WRONG").fadeIn(5000,function(){

            $(this).fadeOut(5000,function(){
                $(".right-answer").removeAttr("disabled");
            });
        })

    });

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the issue of AJAX and PHP script not returning to the original form after posting

I've set up a form with Ajax to send data to a simple PHP script for troubleshooting purposes, but I'm facing an issue where the data is not being posted back to the original HTML form. Below are the two scripts: <!DOCTYPE html> <htm ...

Choose from the options provided to display the table on the screen

One of the challenges I am facing involves a table with two columns and a select option dropdown box. Each row in the table is associated with a color - green indicates good, red indicates bad, and so on. My goal is to have all values displayed when the pa ...

Customizing CSS by replacing link file styles with code in the HEAD section

UPDATE: After further examination, it appears that the code is functioning correctly. My apologies for any confusion. I am attempting to override certain CSS rules on specific pages. Below is an example of my approach. Can anyone confirm if this is a vali ...

Elasticsearch query fails to execute when encountering a special character not properly escaped

I am facing an issue with querying a keyword that includes a dot (.) at the end. While the query works perfectly on Kibana's console, it fails to execute on my application's function. The following function is responsible for creating the query b ...

Finding the Origin and Automatically Forwarding

I'm having an issue with my code. It's not functioning as expected. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml ...

Telerik Border RadCheckBox

Is there a way to get rid of the black border that shows up when the checkbox is clicked in Bootstrap 4.4.1? Check out this image for reference ...

Tips for ensuring consistent position and size across various window dimensions with CSS

Can someone please assist me in centering an image in the window and ensuring it stays the same size regardless of the window size? I'm unsure how to accomplish this, any tips or guidance would be greatly appreciated. ...

Arranging Bootstrap divs for optimal display on mobile devices

Check out my simple jsfiddle demonstration here. I'm looking for a solution where the image appears above the text on smaller window sizes, instead of dropping below due to the HTML order. Currently, I am considering having duplicate images (one abo ...

In a React application, adjusting the main container height to 100vh results in the window.scrollY position being set to

I was facing problems with flashing on the Safari/Firefox browsers. By setting the main container height to max-height: 100vh, I managed to resolve the flickering issue. However, I am also looking for a solution to keep track of the window.scrollY positi ...

Creating a delayed queue using RxJS Observables can provide a powerful and

Imagine we have a line of true or false statements (we're not using a complicated data structure because we only want to store the order). Statements can be added to the line at any time and pace. An observer will remove items from this line and make ...

Implementing Voting Functionality with Ajax Submission

Can anyone help me with getting ajax functionality to work properly for my Acts_As_Votable buttons? Here's the current code I have: post_controller.rb def favorite @post = Post.find(params[:id]) @post.upvote_from current_user respond_to do |f ...

employing variable in front of json notation

For my github-gist project, I am using JavaScript and AJAX to customize the file name. Here is the JSON data I am working with: var data = { "description": gist_description, "public": true, "files": { "file.txt" : { "content": gist_conten ...

JavaScript can be utilized to alter the style of a cursor

I'm currently developing a browser game and incorporating custom cursors into it. To ensure the custom cursor is displayed throughout the entire page, I've applied it in my CSS (though setting it up for 'body' occasionally reverts back ...

Can a props be retrieved and passed as an argument to a function?

My goal is to retrieve a prop from MapsStateToProps using react-redux's connect and then pass it to a child component. This prop serves as an argument for a function, which in turn returns something that becomes the state of the child component. Alth ...

Having trouble with my Angular subscription - not behaving as I anticipated

I am facing an issue on my shop page where I have a FilterBarComponent as a child component. On initialization, I want it to emit all the categories so that all products are rendered by default. However, on my HomePageComponent, there is a button that allo ...

Converting a time string into a date object using JavaScript and AngularJS

After receiving the string from the REST API as "20160220", I am aiming to format it as "20/02/2016". Utilizing AngularJS for this task, I understand that I will need to implement a filter. I have already attempted the code snippet below: app.filter(&apo ...

Specific phrase enclosed within div element

How can I concatenate three strings together when one of them is enclosed within a strong tag? The result is not what I expected. Can you identify the issue here? Result: There is no Colors for <strong>blah</strong> in the database. Expecte ...

CodeIgniter PHP carousel not populating with database values

My website features carousel sliders to showcase images and their values. There are three carousels on the homepage, all sharing the same code. However, the first carousel is not displaying the name and price like the second and third carousels. You can se ...

Transferring information between Express and React through the Contentful API

I have embarked on a journey to explore Contentful's headless CMS, but I am encountering a challenge with their API client. My goal is to combine Express with React for server-side rendering, and I am utilizing this repository as my starting point. S ...

Force a video element to play by using React Context Provider

I've been incorporating the React Context in a similar manner here, and I'm facing a challenge on how to update my video element (to play the video). Shouldn't the components automatically update when they receive the prop? Below is a simpl ...