This content originally appeared on DEV Community and was authored by Muhammad Tayyab Sheikh
Ever encountered as situation where you had to wait for some HTML element in a webpage before you can execute a function?
Wait no more, because wait-for-it.js
is at your service.
What does it offer?
It offers you a much simpler way to execute a function (callback) when your specified (selector) element gets added to the page.
## How does it work?
It uses nothing else but your very own JavaScript to accomplish the task i.e. JavaScript's MutationObserver
API. You can have a detailed look at its code here:
cstayyab / wait-for-it.js
A JavaScript Library that allows you to execute function when a certain element gets added to the document
How do I use it?
Step 1: Include it in your webpage using jsDelivr CDN
<script src="https://cdn.jsdelivr.net/gh/cstayyab/wait-for-it.js@0.1.0/wait-for-it.js"></script>
Step 2: Register a selector
to listen:
waitForElement('#food', function () {
alert("Your Food is Here!");
});
Step 3: Voilà!!
Feel free to star and fork it on GitHub
This content originally appeared on DEV Community and was authored by Muhammad Tayyab Sheikh
Muhammad Tayyab Sheikh | Sciencx (2021-05-07T00:26:09+00:00) “Let me wait-for-it and call you back”, said wait-for-it.js. Retrieved from https://www.scien.cx/2021/05/07/let-me-wait-for-it-and-call-you-back-said-wait-for-it-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.