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

269
Visualizações
Disable a function for a specific time

I currently have a function that scans a barcode or QR code and returns the result:

    function onScanSuccess(decodedText, decodedResult) {
        console.log(`Code scanned = ${decodedText}`, decodedResult);
    }
    var html5QrcodeScanner = new Html5QrcodeScanner(
        "qr-reader", { fps: 10, qrbox: 250 });
    html5QrcodeScanner.render(onScanSuccess);

It works great but the problem i have is that it scans the code very fast and multiple times.

enter image description here

Is there a way to timeout the function so it only return the scans every second?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The easiest way to do that is to set a flag variable and console.log the result if the flag is true. something like that:

    var flag = true;
    function onScanSuccess(decodedText, decodedResult) {
        if(flag){
            console.log(`Code scanned = ${decodedText}`, decodedResult);
            flag = false;
        }
    }

    setInterval(()=>flag=true, 1000);

    var html5QrcodeScanner = new Html5QrcodeScanner(
        "qr-reader", { fps: 10, qrbox: 250 });
    html5QrcodeScanner.render(onScanSuccess);

There's another way too. we can change the flag with setTimeout in the onScanSuccess function.

    var flag = true;
    function onScanSuccess(decodedText, decodedResult) {
        if(flag){
            console.log(`Code scanned = ${decodedText}`, decodedResult);
            flag = false;
            setTimeout(()=>flag=true, 1000);
        }
    }

    var html5QrcodeScanner = new Html5QrcodeScanner(
        "qr-reader", { fps: 10, qrbox: 250 });
    html5QrcodeScanner.render(onScanSuccess);
about 4 years ago · Juan Pablo Isaza Relatório

0

First of all, thanks for all the answers and comments.

In the end it worked with the tweaking of the fps. I changed the fps: 10 to fps: 1. Thanks @nicael for that answer.

var html5QrcodeScanner = new Html5QrcodeScanner(
    "qr-reader", { fps: 1, qrbox: 250 });
html5QrcodeScanner.render(onScanSuccess);

If you searched for an answer to disable a function for a specific time, look at @Alireza Jahandoost answer that one's perfect for that case.

about 4 years ago · Juan Pablo Isaza Relatório
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