Skip to content

Instantly share code, notes, and snippets.

@riix
Created November 24, 2017 03:31
Show Gist options
  • Save riix/a1be663505136e1f40f650e2ad1acb0a to your computer and use it in GitHub Desktop.
Save riix/a1be663505136e1f40f650e2ad1acb0a to your computer and use it in GitHub Desktop.
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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment