control-timeout
Advanced tools
Comparing version
@@ -87,3 +87,3 @@ // Generated by CoffeeScript 1.10.0 | ||
Timeout.prototype.add = function(id, action, delay) { | ||
Timeout.prototype.add = function(id, action, delay, run) { | ||
var settings, timeout; | ||
@@ -105,2 +105,5 @@ settings = types.forceObject(id); | ||
} | ||
if (run || settings.run) { | ||
this.run(id); | ||
} | ||
return this; | ||
@@ -107,0 +110,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
(function(){var t,i;i=require("types.js"),t=function(){function t(i){this.timeout={},this.running={},this.delay=Math.abs(i)||t.delay}return t.log=i.forceFunction("undefined"!=typeof console&&null!==console?console.log:void 0),t.delay=0,t.prototype.exists=function(t){return this.timeout.hasOwnProperty(t)},t.prototype.isRunning=function(t){return this.running.hasOwnProperty(t)},t.prototype.stopOne=function(t){return clearTimeout(this.running[t]),delete this.running[t],this},t.prototype.stop=function(t){if(t)this.stopOne(t);else for(t in this.running)this.stopOne(t);return this},t.prototype.setTimeout=function(t,i,n){return this.running[t]=setTimeout(function(n){return function(){return delete n.running[t],i()}}(this),n),this},t.prototype.run=function(t){var n,e;if(i.isString(t)&&this.exists(t)&&!this.isRunning(t))this.setTimeout(t,this.timeout[t].action,this.timeout[t].delay);else{n=this.timeout;for(t in n)e=n[t],this.isRunning(t)||this.setTimeout(t,e.action,e.delay)}return this},t.prototype.removeAll=function(t){return t&&this.stop(),this.timeout={},this},t.prototype.remove=function(n){return(n=i.forceString(n))?(this.stopOne(n),delete this.timeout[n]):t.log("cannot remove invalid or non-existing timeout!"),this},t.prototype.add=function(n,e,o){var r,s;return r=i.forceObject(n),i.forceString(n,r.id)?(s={action:i.forceFunction(e,r.action),delay:i.forceNumber(o,0)||i.forceNumber(r.delay,this.delay)},n=i.forceString(n)||r.id,this.exists(n)?t.log("cannot add timeout, id: "+n+" exists already!"):this.timeout[n]=s,this):(t.log("cannot add timeout, invalid or missing arguments!"),this)},t}(),module.exports=t}).call(this); | ||
(function(){var t,i;i=require("types.js"),t=function(){function t(i){this.timeout={},this.running={},this.delay=Math.abs(i)||t.delay}return t.log=i.forceFunction("undefined"!=typeof console&&null!==console?console.log:void 0),t.delay=0,t.prototype.exists=function(t){return this.timeout.hasOwnProperty(t)},t.prototype.isRunning=function(t){return this.running.hasOwnProperty(t)},t.prototype.stopOne=function(t){return clearTimeout(this.running[t]),delete this.running[t],this},t.prototype.stop=function(t){if(t)this.stopOne(t);else for(t in this.running)this.stopOne(t);return this},t.prototype.setTimeout=function(t,i,n){return this.running[t]=setTimeout(function(n){return function(){return delete n.running[t],i()}}(this),n),this},t.prototype.run=function(t){var n,e;if(i.isString(t)&&this.exists(t)&&!this.isRunning(t))this.setTimeout(t,this.timeout[t].action,this.timeout[t].delay);else{n=this.timeout;for(t in n)e=n[t],this.isRunning(t)||this.setTimeout(t,e.action,e.delay)}return this},t.prototype.removeAll=function(t){return t&&this.stop(),this.timeout={},this},t.prototype.remove=function(n){return(n=i.forceString(n))?(this.stopOne(n),delete this.timeout[n]):t.log("cannot remove invalid or non-existing timeout!"),this},t.prototype.add=function(n,e,o,r){var s,u;return s=i.forceObject(n),i.forceString(n,s.id)?(u={action:i.forceFunction(e,s.action),delay:i.forceNumber(o,0)||i.forceNumber(s.delay,this.delay)},n=i.forceString(n)||s.id,this.exists(n)?t.log("cannot add timeout, id: "+n+" exists already!"):this.timeout[n]=u,(r||s.run)&&this.run(n),this):(t.log("cannot add timeout, invalid or missing arguments!"),this)},t}(),module.exports=t}).call(this); |
{ | ||
"name": "control-timeout", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A timeout class for controlling one or multiple timeouts", | ||
@@ -5,0 +5,0 @@ "main": "control-timeout.min.js", |
#control-timeout | ||
####A timeout class for controlling one or multiple timeouts. | ||
<br/> | ||
###Features: | ||
- create one or multiple timeouts within the same context | ||
- have a common delay or override it for specific timeouts | ||
- start and stop one or all timeouts at once | ||
- remove timeouts from the context | ||
- can substitute setInterval for node.js | ||
- dynamically type safe | ||
<br/> | ||
--- | ||
###Usage | ||
First install: `npm install --save control-timeout` | ||
<br/> | ||
```javascript | ||
var Timeout= require( 'control-timeout' ); | ||
// create a timeout instance/context with a default timeout of 3 seconds | ||
var timeout= new Timeout( 3000 ); | ||
// add a timeout to the context without running it right away | ||
timeout.add({ | ||
id : 'first' | ||
// declare an action to be taken in case the timeout is reached | ||
,action : () => console.log( 'first timeout has been triggered' ) | ||
// override the context timeout for this specific timeout | ||
,delay : 4000 | ||
}); | ||
// create a timeout that will remove another one on timeout | ||
timeout.add({ | ||
id : 'cancel-first' | ||
,action : timeout.remove.bind( timeout, 'first' ) | ||
}); | ||
// simulate setInterval, but controll times | ||
var count= 0; | ||
var times= 3; | ||
timeout.add({ | ||
id : times+ '-times' | ||
,action : function(){ | ||
console.log( ++count+ '/'+ times ); | ||
if ( count < times ) timeout.run( times+ '-times' ) | ||
} | ||
,delay : 1000 | ||
}); | ||
// can add like this too | ||
timeout.add( 'test', () => console.log('will I run?') ); | ||
// run all timeouts for this context | ||
timeout.run(); | ||
// 1/3 | ||
// 2/3 | ||
// 3/3 | ||
// stop and remove test from context | ||
timeout.remove( 'test' ); | ||
// etc.. | ||
``` | ||
--- | ||
I will add complete API and some more examples soon. | ||
--- | ||
###license | ||
@@ -84,0 +7,0 @@ |
Sorry, the diff of this file is not supported yet
99
3.13%9515
-13.07%10
-88.51%