I am using jquery and I want to execute a function after counting the number of children of an element. My element has a child and I want to execute a function after it became 2 or more. This child appending is handled by some other JavaScript file and I just want to listen for adding a child in that element. That append child happens after an async operation so it may not be executed at the time of checking children count. Here is what I tried:
let i = 0;
const interVal = setInterval(() => {
const parent = $(".parent");
if (parent && parent.length > 1) {
console.log("Child has been appended.");
} else {
console.log("Child has not been appended!");
if (i >= 50000) {
console.error("A lot of conditions executed and cannot bother browser anymore.");
clearInterval(interVal);
}
}
i++;
}, 1);
This code has a problem. It is checking after a period of time. What can I replace with it?