"Let me wait-for-it and call you back", said wait-for-it.js

cstayyab

Muhammad Tayyab Sheikh

Posted on May 7, 2021

"Let me wait-for-it and call you back", said wait-for-it.js

Ever encountered a 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:

GitHub logo cstayyab / wait-for-it.js

A JavaScript Library that allows you to execute function when a certain element gets added to the document

Where can I possibly use it?

Suppose you are using an external library that makes changes to your page content and you want to execute a function when these changes are made but the library itself does not emit any kind of event.

One way to handle this problem is that you make your own copy of the library and edit that library to suit your need but there can be different restrictions with this approach. For example,

  1. You might have to manually update your version every time the library is updated.
  2. That library some how restricts you to use it when it's not being used from their server.

In scenarios such as mentioned above the only code you can control is yours. So what you can do is that you include wait-for-it.js in your code which can wait for changes to content and if the CSS selector you have specified appears, it will simply run the callback function you have specified. This way even if that particular library is updated you won't have to make any changes to your code.

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.1/wait-for-it.js"></script>
Enter fullscreen mode Exit fullscreen mode

Step 2: Register a selector to listen:

waitForElement('#food', function () {
    alert("Your Food is Here!");
});
Enter fullscreen mode Exit fullscreen mode

Step 3: Voilà!!


Update v0.1.1

Now you can specify a third parameter timeout to waitForElement. If the specified element does not appear in the given time, your callback function will be called with a timeout = true argument instead and the selector will also be removed from the queue. Here is an example:

waitForElement('#NonExistentSelector', function (timeout) {
    if(timeout === true) {
        console.log('As Expected, Element with #NonExistentSelector did not appear in 1000 milliseconds.');
        return;
    }
    console.log('This is not possible at all!');
}, 1000);
Enter fullscreen mode Exit fullscreen mode



Feel free to star and fork it on GitHub
💖 💪 🙅 🚩
cstayyab
Muhammad Tayyab Sheikh

Posted on May 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related