3

This is my code:

$(".btn").click(function() {
  var userChosenColor = $(this).attr("id");
  animatePress(userChosenColor);
})

function animatePress(currentColor) {
  $("." + currentColor).addClass("pressed");
}

setTimeout(animatePress,2000);

So when I press the button on my website, the "pressed" class is applied (to make it darker) but the setTimeout doesn't work to make it revert back to its original colour. I followed the layout for the functions carefully so I don't know why it's not working. Thanks in advance

Edit: This is my jquery script in my HTML, could it be because of that?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" charset="utf-8"></script>
6
  • 1
    the setTimeout is NOT inside the click function. Also could be toogleClass instead addClass as the timeout will not remove the class effect Commented Jun 24, 2020 at 7:42
  • Why should it be inside? Commented Jun 24, 2020 at 7:44
  • Actually I just tried putting it inside the click function, and it still doesn't timeout Commented Jun 24, 2020 at 7:44
  • Could you make a reproducible example? Commented Jun 24, 2020 at 7:45
  • @argonx How do you want to put the timeout relative to when you clicked, if you don't start the timeout when the button is clicked? (Right now it will be scheduled as soon as the code is ran, which is presumably on page load.) Commented Jun 24, 2020 at 7:46

2 Answers 2

1

This should work:

$(".btn").click(function() {
    var userChosenColor = $(this).attr("id");
    animatePress(userChosenColor);
    setTimeout(() => animatePress(userChosenColor),2000);
})

function animatePress(currentColor) {
  $("." + currentColor).toggleClass("pressed");
}
Sign up to request clarification or add additional context in comments.

7 Comments

@Ivar True.Edited the answer, sry
Ivar, do you mean that setTimeout does not work with change in colour?
@argonx I have edited 30s ago, did u try after i edited?
@Jon yes it workkss thank you! But I don't understand the "() =>" what does that mean?
|
0

You setTimeout is outside of the click which will not execute like that as its does not know where the userChoosenColor is:

Try this code should just work fine.

$(".btn").click(function() {
  var userChosenColor = $(this).attr("id");
  setTimeout(function() {
    animatePress(userChosenColor)
  }, 2000);
})

function animatePress(currentColor) {
  $("." + currentColor).toggleClass("pressed");
  console.log('I am Called')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn">Click Me</button>

6 Comments

Now it has another effect, the "pressed" class is added after 2 seconds
@argonx is not that what you wanted to add pressed after two seconds ?
@argonx use .toggleClass() insread if you are changing the effect after two seconds. .addClass will just add and will not disappear after pressed class is added.
@argonx No worries. Glad that i could help. The accepted answer is same as mine. I said the same thing to use toggleClass() instead. Glad that we helped you :)
@argonx Thanks Heaps. I appreciate it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.