riix
11/24/2017 - 3:31 AM

onVisible

onVisible

/**
* function $.fn.onVisible runs callback function once the specified element is visible.
* callback: A function to execute at the time when the element is visible.
* example: $(selector).onVisible(callback);
*/

(function($) {
    $.fn.onVisible = function(callback) {
        var self = this;
        var selector = this.selector;

        if (self.is(":visible")) {
            callback.call(self);
        } else {
            timer = setInterval(function() {
                if ($(selector).is(":visible")) {
                    callback.call($(selector));
                    clearInterval(timer);
                }
            }, 50);
        }
    }
}(jQuery));