global.js 843 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. function Timer(callback, delay, paused) {
  2. var timerId, start, remaining = delay;
  3. var timeWhenPaused = 0;
  4. var timePaused = Date.now();
  5. this.pause = function () {
  6. clearTimeout(timerId);
  7. remaining -= Date.now() - start;
  8. timePaused = Date.now();
  9. };
  10. this.resume = function () {
  11. start = Date.now();
  12. clearTimeout(timerId);
  13. timerId = setTimeout(callback, remaining);
  14. timeWhenPaused += Date.now() - timePaused;
  15. };
  16. this.resetTimeWhenPaused = function() {
  17. timeWhenPaused = 0;
  18. };
  19. this.timeWhenPaused = function () {
  20. return timeWhenPaused;
  21. };
  22. if (paused === false) {
  23. this.resume();
  24. }
  25. }
  26. module.exports = {
  27. io: null, // Socket.io
  28. db: null, // Database
  29. htmlEntities: function(str) {
  30. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  31. },
  32. Timer: Timer
  33. };