Make function wait until element exists

前端 未结 11 1914
一向
一向 2020-11-28 17:40

I\'m trying to add a canvas over another canvas – how can I make this function wait to start until the first canvas is created?

function PaintObject(brush) {         


        
11条回答
  •  孤独总比滥情好
    2020-11-28 18:01

    You can check if the dom already exists by setting a timeout until it is already rendered in the dom.

    var panelMainWrapper = document.getElementById('panelMainWrapper');
    setTimeout(function waitPanelMainWrapper() {
        if (document.body.contains(panelMainWrapper)) {
            $("#panelMainWrapper").html(data).fadeIn("fast");
        } else {
            setTimeout(waitPanelMainWrapper, 10);
        }
    }, 10);
    

提交回复
热议问题