YashuC ッ
YashuC ッ

Reputation: 81

How to create a counter from user input?

Hello I want your help in something. So I have this html code below and I want create a counter in the div with class "counter". Basically the user will enter the number and the counter will start from 0 to that number and reset back to 0. I didn't quite know that much of Javascript. I want to show the counter like this : enter image description here. Please help me.

<!DOCTYPE html>
<html>
<head>
    <style>
    .main{  width: 1000px;
            height: 250px;
            margin: 0 auto;
            background-color: #FBAB7E;
            background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);

        }
    
    h2{ 
            color: white;
            padding-top: 45px;
            text-align: center;
            text-shadow: 1px 1px 2px #000000;
        }
    
    .count{
            padding-left: 375px;
            padding-top: 50px;
        }
    
    .counter{
                background-color: gray;
                margin: 0 auto;
                margin-top: 50px;
                width: 750px;
                height: 150px;
        }
    p {
                
                font-size: 75px;
                text-align: center;
                padding-top: 25px;
        }
    </style>
<body>

<div class="main">
    <h2> ENTER THE COUNT FROM '1' TO '9999' </h2>
    <div class="count">
        <input type="number" name="number" id="value">
        <button type="submit" name="submit" id="button" onclick= > <b> Enter Counter </b> </button>
    </div>
</div>

<div class="counter">
 <p>9 9 9 9</p>
</div>

</body>
</html>


<script>
/** HELP HERE! /***
</script>

Upvotes: 0

Views: 1199

Answers (1)

Rifat Bin Reza
Rifat Bin Reza

Reputation: 2781

let timer = null; //to make a singleton timer
function startCounter() {
  if (timer) return;
  let timeVal = parseInt(document.getElementById('value').value);
  if (timeVal > 0 && timeVal < 1000) { //validate the timer input
    timer = setInterval(function () {
      if (timeVal < 0) { // if the timer is less than 0, then stop it and clear it up
        clearInterval(timer);
        document.getElementById('counter-label').textContent = '9 9 9 9';
        timer = null;
        return;
      }
      document.getElementById('counter-label').textContent = timeVal.toString().padStart(4, '0').split('').join(' '); // format the timer label text
      timeVal--;
    }, 1000);
  }
};
<!DOCTYPE html>
<html>
<head>
    <style>
    .main{  width: 1000px;
            height: 250px;
            margin: 0 auto;
            background-color: #FBAB7E;
            background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);

        }
    
    h2{ 
            color: white;
            padding-top: 45px;
            text-align: center;
            text-shadow: 1px 1px 2px #000000;
        }
    
    .count{
            padding-left: 375px;
            padding-top: 50px;
        }
    
    .counter{
                background-color: gray;
                margin: 0 auto;
                margin-top: 50px;
                width: 750px;
                height: 150px;
        }
    p {
                
                font-size: 75px;
                text-align: center;
                padding-top: 25px;
        }
    </style>
<body>

<div class="main">
    <h2> ENTER THE COUNT FROM '1' TO '9999' </h2>
    <div class="count">
        <input type="number" name="number" id="value">
        <button type="submit" name="submit" id="button" onclick="startCounter()" > <b> Enter Counter </b> </button>
    </div>
</div>

<div class="counter">
 <p id="counter-label">9 9 9 9</p>
</div>

</body>
</html>

Upvotes: 1

Related Questions