Last active
July 7, 2016 01:01
-
-
Save d0minicw0ng/329cac79ce5783110b56 to your computer and use it in GitHub Desktop.
jquery.onvisible.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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