Guest User

Untitled

a guest
Apr 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. function Stopwatch () {
  2. var startTime = 0;
  3. var endTime = 0;
  4. var running = 0;
  5. var duration = 0;
  6.  
  7. this.start = function() {
  8. if (running) {
  9. throw new Error('Stopwatch has alredy started');
  10. }
  11.  
  12. startTime = new Date();
  13. running = true;
  14. };
  15.  
  16. this.stop = function() {
  17. if (!running) {
  18. throw new Error('Stopwatch is not started');
  19. }
  20.  
  21. endTime = new Date();
  22. running = false;
  23. duration = endTime.getTime() - startTime.getTime();
  24. this.duration = duration / 1000;
  25. };
  26.  
  27. this.reset = function() {
  28. startTime = 0;
  29. endTime = 0;
  30. running = 0;
  31. duration = 0;
  32. this.duration = 0;
  33. };
  34.  
  35. Object.defineProperty(this, 'duration', {
  36. get: function() {
  37. return duration;
  38. },
  39.  
  40. set: function(val) {
  41. duration = val;
  42. }
  43. })
  44. }
Add Comment
Please, Sign In to add comment