0

I'm trying to run the following jQuery Code.

$('body.player').find('.tab').click(function(){
      $('.playerLoaders').addClass('loading');

      setTimeout(function() {
            if( !$(this).hasClass('active') ){
                 $('.playerLoaders').removeClass('loading');
                 $('.tab-content[data-tab="' + $(this).attr('data-tab') + '"]').addClass('active').siblings().removeClass('active');
                 $(this).addClass('active').siblings().removeClass('active');
            }
            return false;
      },5000);
});

But something is not working quite right.
The div-tag "playerLoaders" is working perfectly, but the tab-content-div is not removing the active class, and adding it to the active tab.

If I delete the TimeOut-function, the tabs are working just fine.

What am I doing wrong?

4
  • Are you waiting 5 seconds to see if things change? .playLoaders will change on the click... everything else waits 5 seconds, then runs IF it doesn't have the .active class. Commented Apr 9, 2016 at 22:31
  • try placing the timeout function after the if statement Commented Apr 9, 2016 at 22:33
  • just what exactly are you trying to do? Commented Apr 9, 2016 at 22:33
  • 'this' is referencing the setTimeout context, and not the outer function context. You should do var self = this; and use self inside the setTimeout function. Commented Apr 9, 2016 at 22:48

1 Answer 1

1

Inside the setTimeout function, 'this' is referencing the setTimeout context, and not the outer function context. You should get a reference to the outer function context. Consider this:

$('body.player').find('.tab').click(function(){
      var self = this;
      $('.playerLoaders').addClass('loading');

      setTimeout(function() {
            if( !$(self).hasClass('active') ){
                 $('.playerLoaders').removeClass('loading');
                 $('.tab-content[data-tab="' + $(self).attr('data-tab') + '"]').addClass('active').siblings().removeClass('active');
                 $(self).addClass('active').siblings().removeClass('active');
            }
            return false;
      },5000);
});

If you want to keep using 'this' inside the setTimeout function, you can also bind it like this:

setTimeout((function() {
            if( !$(this).hasClass('active') ){
                 $('.playerLoaders').removeClass('loading');
                 $('.tab-content[data-tab="' + $(this).attr('data-tab') + '"]').addClass('active').siblings().removeClass('active');
                 $(this).addClass('active').siblings().removeClass('active');
            }
            return false;
}).bind(this),5000);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi carlosdubusm Thank you for the input. The problem is, that know when I click, the .playerLoaders gets the loading class added, but nothing else happens. The TimeOut function, doesn't get activated?
Did you try using 'self' instead of 'this'? Can you post a jsfiddle with the issue?
Hi again @carlosdubusm It was actually working, I had a "." where there shouldn't be one - sorry for that!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.