global.js 982 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. class Timer {
  3. constructor(callback, delay, paused) {
  4. this.callback = callback;
  5. this.delay = delay;
  6. this.paused = paused;
  7. this.timerId = delay;
  8. this.start = delay;
  9. this.remaining = delay;
  10. this.timeWhenPaused = 0;
  11. this.timePaused = Date.now();
  12. }
  13. pause() {
  14. clearTimeout(this.timerId);
  15. this.remaining -= Date.now() - this.start;
  16. this.timePaused = Date.now();
  17. }
  18. ifNotPaused() {
  19. if (!this.paused) {
  20. this.resume();
  21. }
  22. }
  23. resume() {
  24. this.start = Date.now();
  25. clearTimeout(this.timerId);
  26. this.timerId = setTimeout(this.callback, this.remaining);
  27. this.timeWhenPaused = Date.now() - this.timePaused;
  28. }
  29. resetTimeWhenPaused() {
  30. this.timeWhenPaused = 0;
  31. }
  32. timeWhenPaused() {
  33. return this.timeWhenPaused;
  34. }
  35. }
  36. module.exports = {
  37. io: null, // Socket.io
  38. db: null, // Database
  39. htmlEntities: str => {
  40. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  41. },
  42. Timer
  43. };