How to tell when a dynamically created element has rendered

前端 未结 7 1593
我在风中等你
我在风中等你 2020-12-02 12:54

I need to accurately measure the dimensions of text within my web app, which I am achieving by creating an element (with relevant CSS classes), setting its innerHTML

7条回答
  •  忘掉有多难
    2020-12-02 13:21

    This blog post By Swizec Teller, suggests using requestAnimationFrame, and checking for the size of the element.

    function try() {
        if (!$("#element").size()) {
            window.requestAnimationFrame(try);
        } else {
            $("#element").do_some_stuff();
        }
    };
    

    in practice it only ever retries once. Because no matter what, by the next render frame, whether it comes in a 60th of a second, or a minute, the element will have been rendered.

提交回复
热议问题