Wait for animation to finish before calling function
<p>JS:</p>

<pre><code>$.fn.blink = function(times, duration) {
    times = times || 2;

    while (times--) {
        this.fadeTo(duration, 0).fadeTo(duration, 1);
    }      
};
</code></pre>

<p>This makes blinking text. At last blink I want to execute a function so I changed it like this:</p>

<pre><code>$.fn.blink = function(times, duration, callback) {
    times = times || 2;

    while (times--) {
        this.fadeTo(duration, 0).fadeTo(duration, 1);
    }

    if (typeof callback == 'function') { // make sure the callback is a function
        callback.call(this); // brings the scope to the callback
    }
};
</code></pre>

<p>The execution of callback is not at the right place. It seems to be fired when blinking starts. Any ideas how to call the function at last blinking?</p>

<p><a href="http://jsfiddle.net/Cnm2f/2/" rel="nofollow"><strong>FIDDLE</strong></a></p>
