stopwatch.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Stopwatch
  2. var stopwatchInterval = 0; // The interval for our loop.
  3. var stopwatchClock = $(".time"),
  4. stopwatchDigits = stopwatchClock.find('span');
  5. if(Number(localStorage.stopwatchBeginingTimestamp) && Number(localStorage.stopwatchRunningTime)){
  6. var runningTime = Number(localStorage.stopwatchRunningTime) + new Date().getTime() - Number(localStorage.stopwatchBeginingTimestamp);
  7. localStorage.stopwatchRunningTime = runningTime;
  8. startStopwatch();
  9. }
  10. if(localStorage.stopwatchRunningTime){
  11. stopwatchDigits.text(returnFormattedToMilliseconds(Number(localStorage.stopwatchRunningTime)));
  12. }
  13. else{
  14. localStorage.stopwatchRunningTime = 0;
  15. }
  16. function startStopwatch(){
  17. clearInterval(stopwatchInterval);
  18. var startTimestamp = new Date().getTime(),
  19. runningTime = 0;
  20. localStorage.stopwatchBeginingTimestamp = startTimestamp;
  21. // The app remembers for how long the previous session was running.
  22. if(Number(localStorage.stopwatchRunningTime)){
  23. runningTime = Number(localStorage.stopwatchRunningTime);
  24. }
  25. else{
  26. localStorage.stopwatchRunningTime = 1;
  27. }
  28. // Every 100ms recalculate the running time, the formula is:
  29. // time = now - when you last started the clock + the previous running time
  30. stopwatchInterval = setInterval(function () {
  31. var stopwatchTime = (new Date().getTime() - startTimestamp + runningTime);
  32. //console.log(stopwatchTime);
  33. $(".time").find('span').text(returnFormattedToMilliseconds(stopwatchTime));
  34. }, 100);
  35. stopwatchClock.removeClass('inactive');
  36. }
  37. function pauseStopwatch(){
  38. // Stop the interval.
  39. clearInterval(stopwatchInterval);
  40. if(Number(localStorage.stopwatchBeginingTimestamp)){
  41. // On pause recalculate the running time.
  42. // new running time = previous running time + now - the last time we started the clock.
  43. var runningTime = Number(localStorage.stopwatchRunningTime) + new Date().getTime() - Number(localStorage.stopwatchBeginingTimestamp);
  44. localStorage.stopwatchBeginingTimestamp = 0;
  45. localStorage.stopwatchRunningTime = runningTime;
  46. stopwatchClock.addClass('inactive');
  47. }
  48. }
  49. // Reset everything.
  50. function resetStopwatch(){
  51. clearInterval(stopwatchInterval);
  52. stopwatchDigits.text(returnFormattedToMilliseconds(0));
  53. localStorage.stopwatchBeginingTimestamp = 0;
  54. localStorage.stopwatchRunningTime = 0;
  55. stopwatchClock.addClass('inactive');
  56. }
  57. function returnFormattedToMilliseconds(time){
  58. var milliseconds = Math.floor((time % 1000) / 100),
  59. seconds = Math.floor((time/1000) % 60),
  60. minutes = Math.floor((time/(1000*60)) % 60),
  61. hours = Math.floor((time/(1000*60*60)) % 24);
  62. seconds = seconds < 10 ? '0' + seconds : seconds;
  63. minutes = minutes < 10 ? '0' + minutes : minutes;
  64. return hours + ":" + minutes + ":" + seconds;
  65. }