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

253
Visualizações
How to Create a Random Text Generator with Countdown Timer in Javascript?

So, I'm trying to create a random text generator in Javascript using Math.floor and Math.random which I combine with countdown timers using Javascript as well. However, the result after the countdown value has been <= 0 does not appear random text that I have made in the function.

In fact, it appears undefined. How's the solution? The script I created is below.

<button id="btn" style="background-color:red;width:30px;height:30px;"></button>
<script>   
    var timer = 5;  
    var id;
        function create_random_string(string_length){
            var random_string = 'X-';
            var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
            for (var i, i=0; i < string_length; i++){
                random_string += characters.charAt(Math.floor(Math.random()  * characters.length))
            }
        }
    function starButton() {
        this.style.display = 'none';  
        id = setInterval(function () { 
        timer--; 
        if (timer <= 0) {
            clearInterval(id);
            document.getElementById("script").innerHTML = create_random_string(5);
        }  else { 
            document.getElementById("script").innerHTML = timer + " seconds to get Code"; 
        } }, 1000); 
    };  
    var clickbtn = document.getElementById("btn");  
    clickbtn.onclick = starButton; 
</script>
<div id="script"></div> 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You were not returning anything from your function. If you don't return function value how it will get it! Check this now.

 
    var timer = 5;  
    var id;
        function create_random_string(string_length){
        debugger;
            var random_string = 'X-';
            var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
            for (var i, i=0; i < string_length; i++){
                random_string += characters.charAt(Math.floor(Math.random()  * characters.length))
            }
            return random_string;
        }
    function starButton() {
        this.style.display = 'none';  
        id = setInterval(function () { 
        timer--; 
        if (timer <= 0) {
            clearInterval(id);
            document.getElementById("script").innerHTML = create_random_string(5);
        }  else { 
            document.getElementById("script").innerHTML = timer + " seconds to get Code"; 
        } }, 1000); 
    };  
    var clickbtn = document.getElementById("btn");  
    clickbtn.onclick = starButton; 
<button id="btn" style="background-color:red;width:30px;height:30px;"></button>

<div id="script"></div> 

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