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

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

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