I have a div where when I click it, it adds a class of 'playing'. Then, after 10 seconds, I want to add a class of 'finished'.
I have this code, but how do I time it so after 10 seconds, it adds the class of finsihed?
$('.albums img').on('click',function(){
$(this).addClass('playing');
});
Any help is appreciated!
Thanks a ton everyone. I used this question to show ~30 students at HackerYou how to use stackoverflow to get top notch help from the community.
If you have it applied to an element but want to remove it from an element after a designated amount of time has passed, you can do so by using jQuery's . setTimeout() method. var header = $('#header'); header. addClass('blue'); setTimeout(function() { header.
jQuery addClass() MethodThe addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.
Try using setTimeout specifying 10 seconds delay.
$('.albums img').on('click',function(){
var $this = $(this).addClass('playing');
window.setTimeout(function(){
$this.addClass('finsihed');
}, 10000); //<-- Delay in milliseconds
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With