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

JavaScript - Simple Stopwatch

Hue

Cloud Innovator
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
115
Likes
31
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Simple Stopwatch using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Simple Stopwatch</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <center

    ><h1

    id

    =

    "timer"

    style

    =

    "font-size:72px;"

    >

    00:00:00s</

    h1

    ></

    center

    >
  18. <br

    /

    >
  19. <div

    style

    =

    "text-align:center;"

    >
  20. <button

    class

    =

    "btn btn-success"

    onclick

    =

    "startTimer(this)"

    >

    START</

    button

    >
  21. <button

    class

    =

    "btn btn-danger"

    onclick

    =

    "stopTimer()"

    >

    STOP</

    button

    >
  22. <button

    class

    =

    "btn btn-warning"

    onclick

    =

    "resetTimer()"

    >

    RESET</

    button

    >
  23. </

    div

    >
  24. </

    div

    >

  25. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  26. </

    body

    >
  27. </

    html

    >

Creating the Script

This code contains the script of the application. This code will render a virtual stopwatch that can monitor your activity time. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var

    display =

    document.getElementById

    (

    'timer'

    )

    ;
  2. var

    secs =

    0

    ;
  3. var

    mins =

    0

    ;
  4. var

    hrs =

    0

    ;
  5. var

    h =

    ""

    ;
  6. var

    m =

    ""

    ;
  7. var

    s =

    ""

    ;
  8. var

    timer;

  9. function

    countTimer(

    )

    {
  10. secs++;

  11. if

    (

    secs >=

    60

    )

    {
  12. secs =

    0

    ;
  13. mins++;
  14. if

    (

    mins >=

    60

    )

    {
  15. mins =

    0

    ;
  16. hrs++;
  17. }
  18. }

  19. h =

    hrs ?

    hrs >

    9

    ?

    hrs :

    "0"

    +

    hrs :

    "00"

    ;
  20. m =

    mins ?

    mins >

    9

    ?

    mins :

    "0"

    +

    mins :

    "00"

    ;
  21. s =

    secs >

    9

    ?

    secs :

    "0"

    +

    secs;

  22. display.innerHTML

    =

    h+

    ":"

    +

    m+

    ":"

    +

    s+

    "s"

    ;

  23. timerDuration(

    )

    ;
  24. }

  25. function

    timerDuration(

    )

    {
  26. if

    (

    hrs !=

    99

    )

    {
  27. timer =

    setTimeout(

    countTimer,

    100

    )

    ;
  28. }

  29. }

  30. function

    startTimer(

    btn)

    {
  31. btn.setAttribute

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  32. timerDuration(

    )

    ;

  33. }


  34. function

    stopTimer(

    )

    {
  35. document.getElementsByClassName

    (

    'btn-success'

    )

    [

    0

    ]

    .removeAttribute

    (

    'disabled'

    )

    ;
  36. clearTimeout(

    timer)

    ;
  37. }

  38. function

    resetTimer(

    )

    {
  39. document.getElementsByClassName

    (

    'btn-success'

    )

    [

    0

    ]

    .removeAttribute

    (

    'disabled'

    )

    ;
  40. clearTimeout(

    timer)

    ;
  41. display.innerHTML

    =

    "00:00:00s"

    ;
  42. secs =

    0

    ;
  43. mins =

    0

    ;
  44. hrs =

    0

    ;
  45. h =

    ""

    ;
  46. m =

    ""

    ;
  47. s =

    ""

    ;
  48. }

⚙ Live Demo

There you have it we successfully created a Simple Stopwatch using 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!


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

449,193

322,229

322,238

Top