├── README.md ├── example.js ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # Long timeouts 2 | 3 | Long timeout makes it possible to have a timeout or interval that is longer than 24.8 days (2^31-1 milliseconds). 4 | 5 | 6 | ## Usage 7 | 8 | ```js 9 | var lt = require('long-timeout') 10 | 11 | var timeout = lt.setTimeout(function() { 12 | console.log('in 30 days') 13 | }, 1000 * 60 * 60 * 24 * 30) 14 | 15 | var interval = lt.setInterval(function() { 16 | console.log('every 30 days') 17 | }, 1000 * 60 * 60 * 24 * 30) 18 | 19 | 20 | // Clear them 21 | lt.clearTimeout(timeout) 22 | lt.clearInterval(interval) 23 | ``` 24 | 25 | ## Install 26 | 27 | npm install long-timeout 28 | 29 | 30 | ## Licence 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var lt = require('./') 2 | 3 | /* 4 | Timeouts 5 | */ 6 | lt.setTimeout(function() { 7 | console.log('in a long time') 8 | }, Number.MAX_VALUE) 9 | 10 | lt.setTimeout(function() { 11 | console.log('2 seconds') 12 | }, 2000) 13 | 14 | /* 15 | Intervals 16 | */ 17 | lt.setInterval(function() { 18 | console.log('long interval') 19 | }, Number.MAX_VALUE) 20 | 21 | lt.setInterval(function() { 22 | console.log("2 second interval") 23 | }, 2000) 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | var TIMEOUT_MAX = 2147483647; // 2^31-1 3 | 4 | exports.setTimeout = function(listener, after) { 5 | return new Timeout(listener, after) 6 | } 7 | exports.setInterval = function(listener, after) { 8 | return new Interval(listener, after) 9 | } 10 | exports.clearTimeout = function(timer) { 11 | if (timer) timer.close() 12 | } 13 | exports.clearInterval = exports.clearTimeout 14 | 15 | exports.Timeout = Timeout 16 | exports.Interval = Interval 17 | 18 | function Timeout(listener, after) { 19 | this.listener = listener 20 | this.after = after 21 | this.unreffed = false 22 | this.start() 23 | } 24 | 25 | Timeout.prototype.unref = function() { 26 | if (!this.unreffed) { 27 | this.unreffed = true 28 | this.timeout.unref() 29 | } 30 | } 31 | 32 | Timeout.prototype.ref = function() { 33 | if (this.unreffed) { 34 | this.unreffed = false 35 | this.timeout.ref() 36 | } 37 | } 38 | 39 | Timeout.prototype.start = function() { 40 | if (this.after <= TIMEOUT_MAX) { 41 | this.timeout = setTimeout(this.listener, this.after) 42 | } else { 43 | var self = this 44 | this.timeout = setTimeout(function() { 45 | self.after -= TIMEOUT_MAX 46 | self.start() 47 | }, TIMEOUT_MAX) 48 | } 49 | if (this.unreffed) { 50 | this.timeout.unref() 51 | } 52 | } 53 | 54 | Timeout.prototype.close = function() { 55 | clearTimeout(this.timeout) 56 | } 57 | 58 | function Interval(listener, after) { 59 | this.listener = listener 60 | this.after = this.timeLeft = after 61 | this.unreffed = false 62 | this.start() 63 | } 64 | 65 | Interval.prototype.unref = function() { 66 | if (!this.unreffed) { 67 | this.unreffed = true 68 | this.timeout.unref() 69 | } 70 | } 71 | 72 | Interval.prototype.ref = function() { 73 | if (this.unreffed) { 74 | this.unreffed = false 75 | this.timeout.ref() 76 | } 77 | } 78 | 79 | Interval.prototype.start = function() { 80 | var self = this 81 | 82 | if (this.timeLeft <= TIMEOUT_MAX) { 83 | this.timeout = setTimeout(function() { 84 | self.listener() 85 | self.timeLeft = self.after 86 | self.start() 87 | }, this.timeLeft) 88 | } else { 89 | this.timeout = setTimeout(function() { 90 | self.timeLeft -= TIMEOUT_MAX 91 | self.start() 92 | }, TIMEOUT_MAX) 93 | } 94 | if (this.unreffed) { 95 | this.timeout.unref() 96 | } 97 | } 98 | 99 | Interval.prototype.close = function() { 100 | Timeout.prototype.close.apply(this, arguments) 101 | } 102 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "long-timeout", 3 | "version": "0.1.1", 4 | "description": "Long timeout makes it possible to have a timeout or interval that is longer than 24.8 days (2^31-1 milliseconds).", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/tellnes/long-timeout.git" 12 | }, 13 | "author": "Christian Tellnes (http://christian.tellnes.com/)", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/tellnes/long-timeout/issues" 17 | }, 18 | "homepage": "https://github.com/tellnes/long-timeout" 19 | } 20 | --------------------------------------------------------------------------------