Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add class after 10 seconds

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.

like image 413
wesbos Avatar asked Nov 22 '13 01:11

wesbos


People also ask

How do you remove a class after some time?

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.

How to addClass?

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.


1 Answers

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
  });
like image 95
PSL Avatar answered Sep 17 '22 20:09

PSL