2 Replies - 2624 Views - Last Post: 31 May 2013 - 11:30 AM

#1 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

fadeIn() fadeOut() complete function behavior

Posted 31 May 2013 - 10:42 AM

Hi guys.

I'm having trouble understanding the complete function behavior, when I execute the following code:

$(function () {
    
    var numsFaded = 0;
    var box = $("#box");
    var fades = $("#fades");
    var toggle = true;

    fades.text(numsFaded);
    
    var fOut = function () {
        box.fadeOut(250, incrementNums());
    };

    var fIn = function () {
        box.fadeIn(250, incrementNums());
    };
    
    var incrementNums = function () {
        numsFaded++;
        fades.text(numsFaded);
        flip(numsFaded);
    };

    var flip = function (i) {
        if (i < 20) {
            if (toggle === true) {
                toggle = false;
                fIn();
            } else {
                toggle = true;
                fOut();
            }
        }
    };
    
    fOut();
});



My problem is with incrementing value of "fades" element. Why is it incremented to 20 instantly? I expected the value to be incremented after each fade effect is done executing. According to jQuery API documentation, it should execute flip() function after animation. What is happening here, that I don't see (did I mention that this is my first post in JS JQ forums, and I'm new to this?! ;)/> )?

P.S.
search on the forums is not working for me...

Is This A Good Question/Topic? 0
  • +

Replies To: fadeIn() fadeOut() complete function behavior

#2 laytonsdad   User is offline

  • I identify as an attack helicopter!
  • member icon

Reputation: 467
  • View blog
  • Posts: 1,998
  • Joined: 30-April 10

Re: fadeIn() fadeOut() complete function behavior

Posted 31 May 2013 - 11:22 AM

The reason that you are getting 20 at the start is that you are calling the incrementNums() in your callback, what you want to do is pass a reference to it.

Remove the () from both callbacks and it will work fine.

Spoiler

This post has been edited by Dormilich: 31 May 2013 - 04:11 PM
Reason for edit:: fixed typo

Was This Post Helpful? 1
  • +
  • -

#3 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: fadeIn() fadeOut() complete function behavior

Posted 31 May 2013 - 11:30 AM

Great, thanks. It never came to my mind, probably because of fading effects working as expected.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1