Faire fonction d'attendre jusqu'à ce que l'élément existe

J'essaye d'ajouter une toile sur une autre toile – comment puis-je faire de cette fonction d'attendre pour commencer jusqu'à ce que la première toile est créé?

function PaintObject(brush) {

    this.started = false;

    //get handle of the main canvas, as a DOM object, not as a jQuery Object. Context is unfortunately not yet
    //available in jquery canvas wrapper object.
    var mainCanvas = $("#" + brush).get(0);

    //Check if everything is ok
    if (!mainCanvas) {alert("canvas undefined, does not seem to be supported by your browser");}
    if (!mainCanvas.getContext) {alert('Error: canvas.getContext() undefined !');}

    //Get the context for drawing in the canvas
    var mainContext = mainCanvas.getContext('2d');
    if (!mainContext) {alert("could not get the context for the main canvas");}

    this.getMainCanvas = function () {
        return mainCanvas;
    }
    this.getMainContext = function () {
        return mainContext;
    }

    //Prepare a second canvas on top of the previous one, kind of second "layer" that we will use
    //in order to draw elastic objects like a line, a rectangle or an ellipse we adjust using the mouse
    //and that follows mouse movements
    var frontCanvas = document.createElement('canvas');
    frontCanvas.id = 'canvasFront';
    //Add the temporary canvas as a second child of the mainCanvas parent.
    mainCanvas.parentNode.appendChild(frontCanvas);

    if (!frontCanvas) {
        alert("frontCanvas null");
    }
    if (!frontCanvas.getContext) {
        alert('Error: no frontCanvas.getContext!');
    }
    var frontContext = frontCanvas.getContext('2d');
    if (!frontContext) {
        alert("no TempContext null");
    }

    this.getFrontCanvas = function () {
        return frontCanvas;
    }
    this.getFrontContext = function () {
        return frontContext;
    }
  • Lorsque vous créez la toile sur un clic, l'exécution de la fonction ou du déclenchement d'un événement qui s'exécute dans un gestionnaire qui exécute la fonction. il n'est pas intégré dans le cross-browser événement qui se produit lorsqu'un élément est disponible.
  • double possible de Comment faire pour attendre jusqu'à ce qu'un élément existe?
InformationsquelleAutor Steven | 2013-04-22