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

Stopwatch in Bootstrap Templates

TheDealer

OVA Enthusiast
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
117
Likes
127
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, we are going to learn how to create Stopwatch in Bootstrap Templates. The feature of this simple tutorial has the function to start the stopwatch, to pause, to resume, and to reset the stopwatch back to zero. If you hit the reset button the timer will automatically back to zero then press the start button to begin.

Look for the Live Demo, click the button below. Thank you.

⚙ Live Demo

Create simple markup for the controls of the timer and display of the stopwatch in the web page. This is a simple source code so all we can easy to learn on how to create simple stopwatch on your web page.

  1. <div

    style

    =

    "width:40%;"

    >
  2. <div

    >
  3. <p

    id

    =

    "stopWatchDisplay"

    style

    =

    "font-size:18px; font-weight:bold; font-family:cursive;"

    >

    Stopwatch Display Here</

    p

    >
  4. </

    div

    >
  5. <div

    id

    =

    "controls"

    >
  6. <button

    id

    =

    "startPause"

    onClick

    =

    "startPause()"

    ><i

    ></

    i

    >

    Start</

    button

    >
  7. <button

    onClick

    =

    "reset()"

    ><i

    ></

    i

    >

    Reset</

    button

    >
  8. </

    div

    >
  9. </

    div

    >

As you can see the source code below, that's the script to control the timer on the web page, where you can start the time and to pause the time in just one click the button.

  1. function

    startPause(

    )

    {
  2. if

    (

    running ==

    0

    )

    {
  3. running =

    1

    ;
  4. increment(

    )

    ;
  5. document.getElementById

    (

    "startPause"

    )

    .innerHTML

    =

    "<i class='glyphicon glyphicon-pause'></i> Pause"

    ;
  6. }

    else

    {
  7. running =

    0

    ;
  8. document.getElementById

    (

    "startPause"

    )

    .innerHTML

    =

    "<i class='glyphicon glyphicon-repeat'></i> Resume"

    ;
  9. }
  10. }

And, this script below, used to reset the time in the stopwatch on the web page. The timer will automatically back to the zero.

  1. function

    reset(

    )

    {
  2. running =

    0

    ;
  3. time =

    0

    ;
  4. document.getElementById

    (

    "startPause"

    )

    .innerHTML

    =

    "Start"

    ;
  5. document.getElementById

    (

    "stopWatchDisplay"

    )

    .innerHTML

    =

    "00:00:00"

    ;
  6. }

Here's the full source code for the timer script.

  1. var

    time =

    0

    ;
  2. var

    running =

    0

    ;

  3. function

    startPause(

    )

    {
  4. if

    (

    running ==

    0

    )

    {
  5. running =

    1

    ;
  6. increment(

    )

    ;
  7. document.getElementById

    (

    "startPause"

    )

    .innerHTML

    =

    "<i class='glyphicon glyphicon-pause'></i> Pause"

    ;
  8. }

    else

    {
  9. running =

    0

    ;
  10. document.getElementById

    (

    "startPause"

    )

    .innerHTML

    =

    "<i class='glyphicon glyphicon-repeat'></i> Resume"

    ;
  11. }
  12. }

  13. function

    reset(

    )

    {
  14. running =

    0

    ;
  15. time =

    0

    ;
  16. document.getElementById

    (

    "startPause"

    )

    .innerHTML

    =

    "Start"

    ;
  17. document.getElementById

    (

    "stopWatchDisplay"

    )

    .innerHTML

    =

    "00:00:00"

    ;
  18. }

  19. function

    increment(

    )

    {
  20. if

    (

    running ==

    1

    )

    {
  21. setTimeout(

    function

    (

    )

    {
  22. time++;
  23. var

    mins =

    Math

    .floor

    (

    time /

    10

    /

    60

    )

    %

    60

    ;
  24. var

    secs =

    Math

    .floor

    (

    time /

    10

    )

    %

    60

    ;
  25. var

    tenths =

    time %

    10

    ;

  26. if

    (

    mins <

    10

    )

    {
  27. mins =

    "0"

    +

    mins;
  28. }
  29. if

    (

    secs <

    10

    )

    {
  30. secs =

    "0"

    +

    secs;
  31. }
  32. document.getElementById

    (

    "stopWatchDisplay"

    )

    .innerHTML

    =

    mins +

    ":"

    +

    secs +

    ":"

    +

    "0"

    +

    tenths;
  33. increment(

    )

    ;
  34. }

    ,

    100

    )

    ;
  35. }
  36. }

⚙ Live Demo

Output

stopwatch2.jpg



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

442,401

317,942

317,951

Top