setTimeout()
is an asynchronous operation. It does not "pause" the execution of your fadeIn()
function. Your .fadeIn()
method returns immediately and then the timer operation happens afterwards.
Returning self
from the setTimeout()
callback does nothing useful. It just returns self
into the inner bowels of the timer subsystem where it is dutifully ignored.
You can support chaining from your .fadeIn()
operation by just adding a return this;
to the end of your method:
HTMLElement.prototype.fadeIn = function( seconds ) {
var self = this;
var miliseconds = seconds * 1000;
var hold = this.style.transition;
this.style.transition = "opacity " + seconds + "s ease";
this.style.opacity = 1;
setTimeout(
function () {
self.style.transition = hold;
}, miliseconds);
return this;
};
But, this won't tell you anything about when the fade operation is actually complete. If you want to know that, you will have to design something else into your method (a callback or a promise or a queue) in order to support animation chaining.
If you're trying to do animation chaining like jQuery does so you could do this:
obj.fadeIn(5).fadeOut(5);
and the two animations would happen in sequence, then a more complicated system will be required. jQuery uses an animation queue where an animation is not necessarily executed immediately, but rather it's added to a queue that is specific to that particular DOM object. Whenever an animation for that particular object completes, it then looks in its queue to see if there's another animation waiting to go for this same object. If so, it starts that animation.
Also, if you're using CSS3 animations and you need to know when the animation is complete, you would need to register for the transitionend event on the object.