Make function wait until element exists

前端 未结 11 1895
一向
一向 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:16

    Another variation of Iftah

    var counter = 10;
    var checkExist = setInterval(function() {
      console.log(counter);
      counter--
      if ($('#the-canvas').length || counter === 0) {
        console.log("by bye!");
        clearInterval(checkExist);
      }
    }, 200);
    

    Just in case the element is never shown, so we don't check infinitely.

提交回复
热议问题