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

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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