Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda