index.js 684 B

123456789101112131415161718192021222324252627282930313233
  1. var TIMEOUT_MAX = 2147483647; // 2^31-1
  2. exports.setTimeout = function(listener, after) {
  3. return new Timeout(listener, after)
  4. }
  5. exports.clearTimeout = function(timer) {
  6. if (timer) timer.close()
  7. }
  8. exports.Timeout = Timeout
  9. function Timeout(listener, after) {
  10. this.listener = listener
  11. this.after = after
  12. this.start()
  13. }
  14. Timeout.prototype.start = function() {
  15. if (this.after <= TIMEOUT_MAX) {
  16. this.timeout = setTimeout(this.listener, this.after)
  17. } else {
  18. var self = this
  19. this.timeout = setTimeout(function() {
  20. self.after -= TIMEOUT_MAX
  21. self.start()
  22. }, TIMEOUT_MAX)
  23. }
  24. }
  25. Timeout.prototype.close = function() {
  26. clearTimeout(this.timeout)
  27. }