Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
¿Cómo crear un generador de texto aleatorio con temporizador de cuenta regresiva en Javascript?

Entonces, estoy tratando de crear un generador de texto aleatorio en Javascript usando Math.floor y Math.random que combino con temporizadores de cuenta regresiva usando Javascript también. Sin embargo, el resultado después de que el valor de la cuenta regresiva haya sido <= 0 no aparece el texto aleatorio que he hecho en la función.

De hecho, parece indefinido. ¿Cómo es la solución? El script que creé está debajo.

 <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 answers
Answer question

0

No estabas devolviendo nada de tu función. Si no devuelve el valor de la función, ¡cómo lo obtendrá! Mira esto ahora.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!