• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Countdown Timer App in JavaScript

bertan

Punchline Warrior
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
82
Likes
116
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
How to Create a Countdown Timer App in JavaScript

Introduction

In this tutorial we will create a How to Create a Countdown Timer App in JavaScript. This tutorial purpose is to teach you how to create a countdown timer. This will cover all the important functionality that allow you to start a timer. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want add a countdown timer. I will give my best to provide you the easiest way of creating this program Countdown Timer. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html form inputs and the Timer. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    a

    >

    </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Create a Countdown Timer App</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-8"

    >
  18. <center><h1

    id

    =

    "result"

    ></

    h1

    ></

    center>
  19. </

    div

    >
  20. <div

    class

    =

    "col-md-4"

    >
  21. <div

    class

    =

    "form-group"

    >
  22. <label

    >

    Enter a seconds</

    label

    >
  23. <input

    type

    =

    "number"

    id

    =

    "count"

    class

    =

    "form-control"

    /

    >
  24. </

    div

    >
  25. <center><button

    class

    =

    "btn btn-primary"

    onclick

    =

    "startTimer(this);"

    >

    Start Timer</

    button

    ></

    center>
  26. </

    div

    >
  27. </

    div

    >
  28. </

    body

    >
  29. <script

    src

    =

    "script.js"

    ></

    script

    >
  30. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will start the timer when you click the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    startTimer(

    btn)

    {
  2. var

    count=

    document.getElementById

    (

    "count"

    )

    .value

    ;

  3. if

    (

    count==

    0

    )

    {
  4. alert(

    "Please enter a valid number!"

    )

    ;
  5. }

    else

    {
  6. document.getElementById

    (

    "count"

    )

    .value

    =

    ""
  7. btn.setAttribute

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  8. var

    counter =

    setInterval(

    function

    (

    )

    {

  9. count =

    count -

    1

    ;
  10. if

    (

    count ==

    -

    1

    )

    {
  11. clearInterval(

    counter)

    ;
  12. btn.removeAttribute

    (

    "disabled"

    )

    ;
  13. return

    ;
  14. }

  15. var

    seconds =

    count %

    60

    ;
  16. var

    minutes =

    Math

    .floor

    (

    count /

    60

    )

    ;
  17. var

    hours =

    Math

    .floor

    (

    minutes /

    60

    )

    ;
  18. minutes %=

    60

    ;
  19. hours %=

    60

    ;

  20. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    "<span class='text-primary'>"

    +

    hours+

    "</span>"

    +

    "H "

    +

    "<span class='text-primary'>"

    +

    minutes+

    "</span>"

    +

    "min "

    +

    "<span class='text-primary'>"

    +

    seconds+

    "</span>"

    +

    "sec"

    ;

  21. }

    ,

    1000

    )

    ;
  22. }
  23. }

In the code above we only created one method that will start the countdown timer called startTimer(). Inside this method we just set multiple conditional statement to handle the runtime of the code structure. To start a timer setInterval() function that will continue to run until it reach the set condition. We create a function that will handle the timer when it reaches to zero by clearing the interval of the timer. We use a variable that set the value of each time component that we will be using for the timer.

Output:

How%20to%20Create%20a%20Countdown%20Timer%20App%20in%20JavaScript%201.png


The How to Create a Countdown Timer App in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Countdown Timer App in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

330,760

330,768

Top