timeoutcontrol
Advanced tools
+13
| { | ||
| "presets": [ | ||
| ["@babel/preset-env", { | ||
| "spec": true, | ||
| "modules": false, | ||
| "useBuiltIns": "usage", | ||
| "forceAllTransforms": true, | ||
| "targets": { | ||
| "ie": 9 | ||
| } | ||
| }] | ||
| ] | ||
| } |
| /*! | ||
| * TimeoutControl | ||
| * (c) 2019 Yong Quan Lim | ||
| * Released under MIT License. | ||
| */ | ||
| (function (global, factory) { | ||
| typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| (global = global || self, global.TimeoutControl = factory()); | ||
| }(this, function () { 'use strict'; | ||
| function _classCallCheck(instance, Constructor) { | ||
| if (!(instance instanceof Constructor)) { | ||
| throw new TypeError("Cannot call a class as a function"); | ||
| } | ||
| } | ||
| function _defineProperties(target, props) { | ||
| for (var i = 0; i < props.length; i++) { | ||
| var descriptor = props[i]; | ||
| descriptor.enumerable = descriptor.enumerable || false; | ||
| descriptor.configurable = true; | ||
| if ("value" in descriptor) descriptor.writable = true; | ||
| Object.defineProperty(target, descriptor.key, descriptor); | ||
| } | ||
| } | ||
| function _createClass(Constructor, protoProps, staticProps) { | ||
| if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
| if (staticProps) _defineProperties(Constructor, staticProps); | ||
| return Constructor; | ||
| } | ||
| var TimeoutControl = | ||
| /*#__PURE__*/ | ||
| function () { | ||
| function TimeoutControl(callback, duration) { | ||
| for (var _len = arguments.length, params = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
| params[_key - 2] = arguments[_key]; | ||
| } | ||
| _classCallCheck(this, TimeoutControl); | ||
| var instErr = 'Failed to create TimeoutControl instance:'; | ||
| if (arguments.length < 2) throw new TypeError("".concat(instErr, " at least 2 arguments required, only ").concat(arguments.length, " supplied.")); | ||
| if (typeof callback !== 'function') throw new TypeError("".concat(instErr, " first argument must be a function.")); | ||
| if (typeof duration !== 'number') throw new TypeError("".concat(instErr, " second argument must be a number greater than 0.")); | ||
| if (duration < 0) throw new RangeError("".concat(instErr, " second argument must be a number greater than 0.")); | ||
| this.params = params; | ||
| this.duration = duration; | ||
| this.timeStart = Date.now(); | ||
| this.timeStop = this.timeStart; | ||
| this.callback = function (callback) { | ||
| // 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); | ||
| this.timeStop = Date.now(); | ||
| }.bind(this, callback); | ||
| Object.defineProperty(this, 'timeLeft', { | ||
| get: function get() { | ||
| var ret = this.duration - (this.timeStop - this.timeStart); | ||
| return ret < 0 ? 0 : ret; | ||
| } | ||
| }); | ||
| Object.defineProperty(this, 'done', { | ||
| get: function get() { | ||
| return this.timeLeft === 0; | ||
| } | ||
| }); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| _createClass(TimeoutControl, [{ | ||
| key: "pause", | ||
| value: function pause() { | ||
| clearTimeout(this.id); | ||
| this.timeStop = Date.now(); | ||
| } | ||
| }, { | ||
| key: "resume", | ||
| value: function resume() { | ||
| this.id = setTimeout(this.callback, this.timeLeft); | ||
| } | ||
| }, { | ||
| key: "clear", | ||
| value: function clear() { | ||
| clearTimeout(this.id); | ||
| this.timeStart = Date.now(); | ||
| this.timeStop = this.timeStart; | ||
| this.id = null; | ||
| } | ||
| }, { | ||
| key: "restart", | ||
| value: function restart() { | ||
| this.clear(); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| }]); | ||
| return TimeoutControl; | ||
| }(); | ||
| return TimeoutControl; | ||
| })); |
| /*! | ||
| * TimeoutControl | ||
| * (c) 2019 Yong Quan Lim | ||
| * Released under MIT License. | ||
| */ | ||
| !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).TimeoutControl=e()}(this,function(){"use strict";function t(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(){function e(t,i){for(var n=arguments.length,r=new Array(n>2?n-2:0),o=2;o<n;o++)r[o-2]=arguments[o];!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);var a="Failed to create TimeoutControl instance:";if(arguments.length<2)throw new TypeError("".concat(a," at least 2 arguments required, only ").concat(arguments.length," supplied."));if("function"!=typeof t)throw new TypeError("".concat(a," first argument must be a function."));if("number"!=typeof i)throw new TypeError("".concat(a," second argument must be a number greater than 0."));if(i<0)throw new RangeError("".concat(a," second argument must be a number greater than 0."));this.params=r,this.duration=i,this.timeStart=Date.now(),this.timeStop=this.timeStart,this.callback=function(t){t.apply(null,this.params),this.timeStop=Date.now()}.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)}var i,n,r;return i=e,(n=[{key:"pause",value:function(){clearTimeout(this.id),this.timeStop=Date.now()}},{key:"resume",value:function(){this.id=setTimeout(this.callback,this.timeLeft)}},{key:"clear",value:function(){clearTimeout(this.id),this.timeStart=Date.now(),this.timeStop=this.timeStart,this.id=null}},{key:"restart",value:function(){this.clear(),this.id=setTimeout(this.callback,this.duration)}}])&&t(i.prototype,n),r&&t(i,r),e}()}); |
+20
-14
| { | ||
| "name": "timeoutcontrol", | ||
| "version": "0.2.0", | ||
| "version": "1.0.0", | ||
| "description": "A JavaScript tool to control setTimeout operations.", | ||
| "main": "index.js", | ||
| "browser": "index.min.js", | ||
| "main": "./lib/timeoutcontrol.js", | ||
| "browser": "./lib/timeoutcontrol.min.js", | ||
| "license": "MIT", | ||
| "author": "Yong Quan Lim <yqlim2394@gmail.com>", | ||
| "scripts": { | ||
| "build": "uglifyjs index.js -o index.min.js --ie8 --compress --mangle reserved=[\"TimeoutControl\"] --comments /license/" | ||
| "build": "npm run rollup && npm run terser", | ||
| "rollup": "rollup -c", | ||
| "terser": "terser -c -m --comments /license/i -o ./lib/timeoutcontrol.min.js ./lib/timeoutcontrol.js" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/core": "^7.2.2", | ||
| "@babel/preset-env": "^7.2.3", | ||
| "rollup": "^1.1.0", | ||
| "rollup-plugin-babel": "^4.3.0", | ||
| "terser": "^3.14.1" | ||
| }, | ||
| "repository": { | ||
@@ -14,2 +25,6 @@ "type": "git", | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/yqlim/timeoutcontrol/issues" | ||
| }, | ||
| "homepage": "https://github.com/yqlim/timeoutcontrol#readme", | ||
| "keywords": [ | ||
@@ -25,12 +40,3 @@ "setTimeout", | ||
| "timeoutpauser" | ||
| ], | ||
| "author": "Yong Quan Lim <yqlim2394@gmail.com>", | ||
| "license": "MIT", | ||
| "bugs": { | ||
| "url": "https://github.com/yqlim/timeoutcontrol/issues" | ||
| }, | ||
| "homepage": "https://github.com/yqlim/timeoutcontrol#readme", | ||
| "devDependencies": { | ||
| "uglify-js": "^3.4.9" | ||
| } | ||
| ] | ||
| } |
+4
-6
@@ -6,3 +6,3 @@ # TimeoutControl | ||
| # Install | ||
| # Installation | ||
@@ -13,6 +13,4 @@ ## NPM | ||
| # API | ||
| # Usage | ||
| ## Invoking | ||
| `TimeoutControl` accepts any parameters that the native `setTimeout` will accept. | ||
@@ -70,7 +68,7 @@ | ||
| Restart the timeout from the beginning. `.timeStart`, `.timeStop` and `.id` will be updated. | ||
| Clear the timeout, then start the timeout with the arguments previously passed to the instance. | ||
| ### .clear() | ||
| Cancel the timeout. `.done` will be updated to `true`. | ||
| Cancel the timeout. | ||
@@ -77,0 +75,0 @@ # Demo |
-132
| /*! | ||
| * TimeoutControl | ||
| * Copyright (c) by Yong Quan Lim | ||
| * Distributed under MIT license: https://github.com/yqlim/TimeoutControl | ||
| */ | ||
| (function(global, factory){ | ||
| if (typeof exports === 'object' && typeof module !== 'undefined') | ||
| module.exports = factory(); | ||
| else if (typeof define === 'function' && define.amd) | ||
| define(factory); | ||
| else | ||
| global.TimeoutControl = factory(); | ||
| })(this, function(){ | ||
| 'use strict'; | ||
| if (typeof setTimeout !== 'function') | ||
| throw new Error('Your environment does not support setTimeout.'); | ||
| // Polyfill for Object.defineProperties | ||
| function defineProperties(obj, desc){ | ||
| var prop; | ||
| for (prop in desc){ | ||
| Object.defineProperty(obj, prop, desc[prop]); | ||
| } | ||
| return obj; | ||
| } | ||
| function TimeoutControl(callback, duration){ | ||
| 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 (typeof callback !== 'function') | ||
| throw new TypeError('Failed to create TimeoutControl instance: first argument must be a callback function.'); | ||
| if (typeof duration !== 'number' || duration < 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 = duration; | ||
| this.timeStart = new Date().getTime(); | ||
| this.timeStop = this.timeStart; | ||
| this.callback = function(callback){ | ||
| // 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); | ||
| this.timeStop = 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; | ||
| } | ||
| }); | ||
| Object.defineProperty(this, 'done', { | ||
| get: function(){ | ||
| return this.timeLeft === 0; | ||
| } | ||
| }); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| defineProperties(TimeoutControl.prototype, { | ||
| pause: { | ||
| writable: true, | ||
| configurable: true, | ||
| value: function(){ | ||
| clearTimeout(this.id); | ||
| this.timeStop = new Date().getTime(); | ||
| } | ||
| }, | ||
| resume: { | ||
| writable: true, | ||
| configurable: true, | ||
| value: function(){ | ||
| this.id = setTimeout(this.callback, this.timeLeft) | ||
| } | ||
| }, | ||
| restart: { | ||
| writable: true, | ||
| configurable: true, | ||
| value: function(){ | ||
| clearTimeout(this.id); | ||
| this.timeStart = new Date().getTime(); | ||
| this.timeStop = this.timeStart; | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| }, | ||
| clear: { | ||
| writable: true, | ||
| configurable: true, | ||
| value: function(){ | ||
| clearTimeout(this.id); | ||
| this.timeStart = new Date().getTime(); | ||
| this.timeStop = this.timeStart; | ||
| this.id = null; | ||
| this.done = true; | ||
| } | ||
| } | ||
| }); | ||
| return TimeoutControl; | ||
| }); |
| /*! | ||
| * TimeoutControl | ||
| * Copyright (c) by Yong Quan Lim | ||
| * Distributed under MIT license: https://github.com/yqlim/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}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
6
20%1
-50%9755
-0.01%5
400%102
-0.97%98
-2%1
Infinity%