Is there a smarter solution to wait until a node exists without using an interval or a fixed retry time?
Currently, I do such like this:
let interval = setInterval(function () {
let neededElement = document.querySelector("small > time");
if (neededElement !== null) {
clearInterval(interval);
...code...
}
}, 100);
What bothers me is that it always waits blindly. If the timing is low, it consumes resources unnecessarily and if it is high, it reacts too slowly to changes. And if the node never exists, it consumes resources unnecessarily in the background.
Probably a mutations observer would be a solution. But seems also not very slick to me, especially if I need this often. Or I would need some kind of (factory) function to build mutation observers.
EDIT:
The main problem of the mutation observer in my use case is the reusability in a script and the accuracy. Because nodes that do not exist yet cannot be observed. Therefore you have to go to higher nodes where it is sure that they exist immediately. But then the observer reacts also on unimportant changes. Depending upon use case these can be very many. So in the end you probably save less resources than with an interval.