最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Callback functionRun a function after adding a class with .addClass - Stack Overflow

programmeradmin0浏览0评论

I'm trying to run a function on the selector that addClass was just ran on after addClass is pleted.

This way made sense to me, but it doesn't seem to be working:

$('.focused').addClass('fadeOutDown', function(){ $(this).remove(); });

How can I run a function after I've ran the addClass function on the same selector?

Chaining runs it at the same time and I haven't tried a set-timeout, but that seem inefficient.

I'm trying to run a function on the selector that addClass was just ran on after addClass is pleted.

This way made sense to me, but it doesn't seem to be working:

$('.focused').addClass('fadeOutDown', function(){ $(this).remove(); });

How can I run a function after I've ran the addClass function on the same selector?

Chaining runs it at the same time and I haven't tried a set-timeout, but that seem inefficient.

Share Improve this question asked Dec 9, 2013 at 4:31 gomangomangogomangomango 6911 gold badge10 silver badges31 bronze badges 9
  • Class is added instantly after $('.focused').addClass('fadeOutDown'); – zerkms Commented Dec 9, 2013 at 4:34
  • addClass is synchronous, you don't need callback here. – Ram Commented Dec 9, 2013 at 4:34
  • @zerkms I know the class is added instantly. I'm trying to run a function after. – gomangomango Commented Dec 9, 2013 at 4:35
  • 2 @gomangomango: put it on the next line. JS is evaluated line by line. The next line is evaluated right after the previous. – zerkms Commented Dec 9, 2013 at 4:35
  • @undefined what does synchronous mean? Then how do I remove the same element that I added the class to after like I am trying to do? – gomangomango Commented Dec 9, 2013 at 4:36
 |  Show 4 more ments

1 Answer 1

Reset to default 4

Try to do this way :

 $('.focused').addClass('fadeOutDown');

 setTimeout(function() {
     $('.focused').remove();
 }, 2000);

   // make sure you provide appropriate time i.e after you get the effect
发布评论

评论列表(0)

  1. 暂无评论