mercredi 1 juin 2016

Best practice to implement callback function when DOM element rendered

While I do not know if it is possible, I am always facing such scenario for a long time without a "good" way to solve it. No matter what technology I used: ASP.NET, plaing JS/JQuery, any other web development frameworks...

The scenario is that:

How to set up a callback function which will fired when some specific DOM element is rendered?

For example, I have a button on the document, and I would like to simulate a click when this button is rendered.

This is the method I used the most for now (probably the dumbest method):

$(function(){
   setTimeout( simulate the click, or w/e I want,  100);
});

I would set the delay by trial & error. While it works most of the time, if I keep refreshing the page, there will be one or two times it fails, as the button is not yet rendered on the document after 100ms. Also I suspect this method is machine / browser dependent...in short it is bad.

I believe there is some much better way to achieve the same effect, what is the common / best practice out there to handle such scneario?

Aucun commentaire:

Enregistrer un commentaire