Guest User

Untitled

a guest
Jul 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. function StopWatch() {
  2.  
  3. let duration = 0;
  4.  
  5. Object.defineProperty(this, 'duration', {
  6. get: function () {
  7. return duration;
  8. }
  9. });
  10.  
  11. let status;
  12.  
  13. let interval;
  14.  
  15.  
  16. this.start = function () {
  17. if (status == 'started')
  18. throw Error('Already started!');
  19. interval = setInterval(timer, 1000);
  20. status = 'started';
  21. }
  22.  
  23. const timer = function () {
  24. duration++;
  25. }
  26.  
  27. this.stop = function () {
  28. if (status == 'stopped' || (typeof status) == 'undefined')
  29. throw Error('Already stopped!')
  30. clearInterval(interval);
  31. status = 'stopped';
  32. }
  33. }
  34. const sw = new StopWatch();
Add Comment
Please, Sign In to add comment