1

I want to create a timer/stop watch in javascript for the following use case:

On clicking the "Play" button, i start the stop watch and on clicking "pause" it's stopped. Then the diff gives me the elapsed time. From this, I should show the song progress time and update it. If the song is fully played, then, the timer should start from the beginning (i.e. 0 level) else, it should start from the state where it has stopped.
Please guide me how to do it in javascript. How to get the current time? (gettime??)

Thanks
Sneha

Smitha
  • 6,110
  • 24
  • 90
  • 161

2 Answers2

0

If you are using this to show the current position of an audio file being played, just use the audio element's currentTime attribute, which is constantly updated with the pointer's location.

Alternatively, the most basic stopwatch simply records new Date().getTime() when started, and again when ended and returns the difference.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Try something like this:

document.getElementById('play').onclick = function() { window.played = +new Date(); }
document.getElementById('pause').onclick = function() {
    var difference = (+new Date() / 1000) - window.played / 1000 < songLengthInSeconds;

    if (difference > songLenthInSeconds)
        // code for start from the beginning
    else

        // code if not fully played yet
}
qwertymk
  • 34,200
  • 28
  • 121
  • 184