🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

timeoutcontrol

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timeoutcontrol - npm Package Compare versions

Comparing version
0.1.1
to
0.2.0
+99
README.md
# TimeoutControl
A JavaScript tool to imitate `setTimeout` functionality to extend control over it.
# Install
## NPM
`npm install timeoutcontrol`
# API
## Invoking
`TimeoutControl` accepts any parameters that the native `setTimeout` will accept.
```javascript
const timeout = new TimeoutControl(callback[, delay[, param1, param2, ...]]);
```
## Properties
### .id
Returns the id of the timeout.
### .params
Returns an array of all params (i.e., `param1`, `param2`, ...).
### .duration
Returns the duration of the timeout.
### .timeStart
Returns the time the timeout starts.
### .timeStop
Returns the time the timeout is paused/stopped.
### .callback
Returns the callback function to run when the timeout ends.
### .timeLeft
Returns the time left before the timeout ends.
### .done
Returns a boolean of whether the timeout has ended.
## Methods
### .pause()
Pause the ongoing timeout.
### .resume()
Resume the paused timeout.
### .restart()
Restart the timeout from the beginning. `.timeStart`, `.timeStop` and `.id` will be updated.
### .clear()
Cancel the timeout. `.done` will be updated to `true`.
# Demo
```javascript
const duration = 5000;
const callback = function(){
console.log(...arguments);
}
const timeout = new TimeoutControl(callback, duration, 1, 2, 3, 4, 5);
/* ...SOME OPERATIONS / CONDITIONS */
timeout.pause();
/* ...SOME OPERATIONS / CONDITIONS */
timeout.resume();
/* ...SOME OPERATIONS / CONDITIONS */
timeout.clear();
/* ...SOME OPERATIONS / CONDITIONS */
timeout.restart();
```
+21
-52

@@ -49,62 +49,31 @@ /*!

defineProperties(this, {
this.params = Array.prototype.slice.call(arguments, 2);
this.duration = duration;
this.timeStart = new Date().getTime();
this.timeStop = this.timeStart;
// id of timeout
id: {
writable: true,
value: null
},
this.callback = function(callback){
// Params to be passed to `callback`
params: {
value: Array.prototype.slice.call(arguments, 2)
},
// Decorate to pass extra params into timeout callback
// Params are passed here instead of directly on setTimeout
// so that this can also act as polyfill for browsers that
// don't support native "rest" param on setTimeout
callback.apply(null, this.params);
duration: {
value: duration
},
this.timeStop = new Date().getTime();
timeStart: {
writable: true,
value: new Date().getTime()
}.bind(this, callback);
Object.defineProperty(this, 'timeLeft', {
get: function(){
var ret = this.duration - (this.timeStop - this.timeStart);
if (ret < 0) return 0;
return ret;
}
});
// Create these props separately
// because they depends on the above props
defineProperties(this, {
timeStop: {
writable: true,
value: this.timeStart
},
timeLeft: {
get: function(){
var ret = this.duration - (this.timeStop - this.timeStart);
return ret < 0 ? 0 : ret;
}
},
callback: {
value: function(callback){
// [null].concat(this.params) to pass extra params into timeout callback
// Params are passed here instead of directly on setTimeout
// so that this can also act as polyfill for browsers that
// don't support rest params on setTimeout
callback.apply(null, this.params);
this.timeStop = new Date().getTime();
}.bind(this, callback)
},
done: {
get: function(){
return this.timeLeft === 0;
}
Object.defineProperty(this, 'done', {
get: function(){
return this.timeLeft === 0;
}
});

@@ -111,0 +80,0 @@

@@ -6,2 +6,2 @@ /*!

*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.TimeoutControl=e()}(this,function(){"use strict";if("function"!=typeof setTimeout)throw new Error("Your environment does not support setTimeout.");function i(t,e){var i;for(i in e)Object.defineProperty(t,i,e[i]);return t}function TimeoutControl(t,e){if(!(this instanceof TimeoutControl))throw new SyntaxError('TimeoutControl must be called with the keyword "new".');if(arguments.length<2)throw new TypeError("Failed to create TimeoutControl instance: at least 2 arguments required, but only "+arguments.length+" present.");if("function"!=typeof t)throw new TypeError("Failed to create TimeoutControl instance: first argument must be a callback function.");if("number"!=typeof e||e<0)throw new RangeError("Failed to create TimeoutControl instance: second argument must be a positive number.");i(this,{id:{writable:!0,value:null},params:{value:Array.prototype.slice.call(arguments,2)},duration:{value:e},timeStart:{writable:!0,value:(new Date).getTime()}}),i(this,{timeStop:{writable:!0,value:this.timeStart},timeLeft:{get:function(){var t=this.duration-(this.timeStop-this.timeStart);return t<0?0:t}},callback:{value:function(t){t.apply(null,this.params),this.timeStop=(new Date).getTime()}.bind(this,t)},done:{get:function(){return 0===this.timeLeft}}}),this.id=setTimeout(this.callback,this.duration)}return i(TimeoutControl.prototype,{pause:{writable:!0,configurable:!0,value:function(){clearTimeout(this.id),this.timeStop=(new Date).getTime()}},resume:{writable:!0,configurable:!0,value:function(){this.id=setTimeout(this.callback,this.timeLeft)}},restart:{writable:!0,configurable:!0,value:function(){clearTimeout(this.id),this.timeStart=(new Date).getTime(),this.timeStop=this.timeStart,this.id=setTimeout(this.callback,this.duration)}},clear:{writable:!0,configurable:!0,value:function(){clearTimeout(this.id),this.timeStart=(new Date).getTime(),this.timeStop=this.timeStart,this.id=null,this.done=!0}}}),TimeoutControl});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.TimeoutControl=e()}(this,function(){"use strict";if("function"!=typeof setTimeout)throw new Error("Your environment does not support setTimeout.");function TimeoutControl(t,e){if(!(this instanceof TimeoutControl))throw new SyntaxError('TimeoutControl must be called with the keyword "new".');if(arguments.length<2)throw new TypeError("Failed to create TimeoutControl instance: at least 2 arguments required, but only "+arguments.length+" present.");if("function"!=typeof t)throw new TypeError("Failed to create TimeoutControl instance: first argument must be a callback function.");if("number"!=typeof e||e<0)throw new RangeError("Failed to create TimeoutControl instance: second argument must be a positive number.");this.params=Array.prototype.slice.call(arguments,2),this.duration=e,this.timeStart=(new Date).getTime(),this.timeStop=this.timeStart,this.callback=function(t){t.apply(null,this.params),this.timeStop=(new Date).getTime()}.bind(this,t),Object.defineProperty(this,"timeLeft",{get:function(){var t=this.duration-(this.timeStop-this.timeStart);return t<0?0:t}}),Object.defineProperty(this,"done",{get:function(){return 0===this.timeLeft}}),this.id=setTimeout(this.callback,this.duration)}return function n(t,e){var i;for(i in e)Object.defineProperty(t,i,e[i]);return t}(TimeoutControl.prototype,{pause:{writable:!0,configurable:!0,value:function(){clearTimeout(this.id),this.timeStop=(new Date).getTime()}},resume:{writable:!0,configurable:!0,value:function(){this.id=setTimeout(this.callback,this.timeLeft)}},restart:{writable:!0,configurable:!0,value:function(){clearTimeout(this.id),this.timeStart=(new Date).getTime(),this.timeStop=this.timeStart,this.id=setTimeout(this.callback,this.duration)}},clear:{writable:!0,configurable:!0,value:function(){clearTimeout(this.id),this.timeStart=(new Date).getTime(),this.timeStop=this.timeStart,this.id=null,this.done=!0}}}),TimeoutControl});
{
"name": "timeoutcontrol",
"version": "0.1.1",
"version": "0.2.0",
"description": "A JavaScript tool to control setTimeout operations.",
"main": "index.js",
"browser": "index.min.js",
"scripts": {

@@ -7,0 +8,0 @@ "build": "uglifyjs index.js -o index.min.js --ie8 --compress --mangle reserved=[\"TimeoutControl\"] --comments /license/"