Skip to content

Instantly share code, notes, and snippets.

@d0minicw0ng
Last active July 7, 2016 01:01
Show Gist options
  • Save d0minicw0ng/329cac79ce5783110b56 to your computer and use it in GitHub Desktop.
Save d0minicw0ng/329cac79ce5783110b56 to your computer and use it in GitHub Desktop.
jquery.onvisible.js
/**
* 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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment