Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
Wait for img.onload function to complete before progressing

I have the following code to check if a YouTube ID is valid. Its a bit of a hack, but works well. It checks the size of the returned thumbnail image. If its not a valid YouTube video, you get the standard placeholder thumbnail thats only 120px. The regular valid thumbnail is 160px so you can test if its a legitimate ID.

The problem I have, is that this if statement doesn't wait for the image to load and test for width, so doesn't catch the change of ok_to_add variable to true (or 1). How can I make the script wait for the img.onload function result and confirm before proceeding? I will need several of these checks all with different criteria for different players.

    var ok_to_add = 0;

    if (this_player_item == 'youtube') {            
        
        var img = new Image();
        img.src = "http://img.youtube.com/vi/" + this_player_id + "/mqdefault.jpg";
        img.onload = function () {
            var img_width = img.width;
            if(img_width < 130) {
                var thisconfirm = confirm("This YouTube ID doesn't appear to be valid. Proceed anyway?");
                if (thisconfirm) {
                    ok_to_add = 1;
                } else {
                    return false;
                }
            } else {
                ok_to_add = 1;
            }
        }

        alert(ok_to_add);

    }
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda