Clock.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. function Clock( autoStart ) {
  5. this.autoStart = ( autoStart !== undefined ) ? autoStart : true;
  6. this.startTime = 0;
  7. this.oldTime = 0;
  8. this.elapsedTime = 0;
  9. this.running = false;
  10. }
  11. Object.assign( Clock.prototype, {
  12. start: function () {
  13. this.startTime = ( typeof performance === 'undefined' ? Date : performance ).now(); // see #10732
  14. this.oldTime = this.startTime;
  15. this.elapsedTime = 0;
  16. this.running = true;
  17. },
  18. stop: function () {
  19. this.getElapsedTime();
  20. this.running = false;
  21. this.autoStart = false;
  22. },
  23. getElapsedTime: function () {
  24. this.getDelta();
  25. return this.elapsedTime;
  26. },
  27. getDelta: function () {
  28. var diff = 0;
  29. if ( this.autoStart && ! this.running ) {
  30. this.start();
  31. return 0;
  32. }
  33. if ( this.running ) {
  34. var newTime = ( typeof performance === 'undefined' ? Date : performance ).now();
  35. diff = ( newTime - this.oldTime ) / 1000;
  36. this.oldTime = newTime;
  37. this.elapsedTime += diff;
  38. }
  39. return diff;
  40. }
  41. } );
  42. export { Clock };