+66
-95
@@ -1,98 +0,69 @@ | ||
| const timerPro = { | ||
| timers: { | ||
| Timeout: {}, | ||
| Interval: {}, | ||
| }, | ||
| timerAll: function() { | ||
| return timerPro.timers; | ||
| }, | ||
| timerExist: function(type, name) { | ||
| return (timerPro.timers[type] && timerPro.timers[type][name]); | ||
| }, | ||
| isArray: function(array) { | ||
| return Array.isArray(array); | ||
| }, | ||
| new: { | ||
| newTimer: function(type, name, timer) { | ||
| if(!timerPro.timerExist(type, name)) timerPro.timers[type][name] = []; | ||
| timerPro.timers[type][name].push(timer); | ||
| }, | ||
| timeout: function(name, func, delay, args = [], reset = false) { | ||
| if(!timerPro.isArray(args)) args = []; | ||
| if(reset) timerPro.reset.timeout(); | ||
| timerPro.new.newTimer("Timeout", name, setTimeout(func, delay, ...args)); | ||
| }, | ||
| interval: function(name, func, delay, args = [], reset = false) { | ||
| if(!timerPro.isArray(args)) args = []; | ||
| if(reset) timerPro.reset.interval(); | ||
| timerPro.new.newTimer("Interval", name, setTimeout(func, delay, ...args)); | ||
| } | ||
| }, | ||
| clear: { | ||
| doClear: function(type, name) { | ||
| if(!timerPro.timerExist(type, name)) return; | ||
| for(timer of timerPro.timers[type][name]) | ||
| { | ||
| if(type == "Timeout") clearTimeout(timer); | ||
| if(type == "Interval") clearInterval(timer); | ||
| } | ||
| (function (global) { | ||
| class TimerPro { | ||
| constructor() { | ||
| this.timeouts = {}; | ||
| this.intervals = {}; | ||
| } | ||
| timerPro.timers[type][name] = []; | ||
| }, | ||
| timeout: function(name) { | ||
| timerPro.clear.doClear("Timeout", name); | ||
| }, | ||
| interval: function(name) { | ||
| timerPro.clear.doClear("Interval", name); | ||
| }, | ||
| timer(type) { | ||
| for(name in timerPro.timers[type]) timerPro.clear.doClear(type, name); | ||
| }, | ||
| all() { | ||
| timerPro.clear.timer("Timeout"); | ||
| timerPro.clear.timer("Interval"); | ||
| } | ||
| }, | ||
| clean: { | ||
| doClean: function(type, name) { | ||
| if(!timerPro.timerExist(type, name)) return; | ||
| let timer, new_list = []; | ||
| for(id in timerPro.timers[type][name]) | ||
| { | ||
| timer = timerPro.timers[type][name][id]; | ||
| if(timer && !timer._destroyed) { | ||
| new_list.push(timer); | ||
| } | ||
| } | ||
| setTimeout(name, func, delay, args = []) { | ||
| this.clearTimeout(name); | ||
| const timeoutId = setTimeout(() => { | ||
| func(...args); | ||
| delete this.timeouts[name]; | ||
| }, delay); | ||
| this.timeouts[name] = this.timeouts[name] || []; | ||
| this.timeouts[name].push(timeoutId); | ||
| return timeoutId; | ||
| } | ||
| timerPro.timers[type][name] = new_list; | ||
| }, | ||
| timeout: function(name) { | ||
| timerPro.clean.doClean("Timeout", name); | ||
| }, | ||
| interval: function(name) { | ||
| timerPro.clean.doClean("Interval", name); | ||
| }, | ||
| timer: function(type) { | ||
| for(name in timerPro.timers[type]) timerPro.clean.doClean(type, name); | ||
| }, | ||
| all: function() { | ||
| timerPro.clean.timer("Timeout"); | ||
| timerPro.clean.timer("Interval"); | ||
| } | ||
| }, | ||
| reset: { | ||
| timeout: function(name) { | ||
| timerPro.clear.timeout(name); | ||
| timerPro.clean.timeout(name); | ||
| }, | ||
| interval: function(name) { | ||
| timerPro.clear.interval(name); | ||
| timerPro.clean.interval(name); | ||
| } | ||
| } | ||
| } | ||
| setInterval(name, func, delay, args = []) { | ||
| this.clearInterval(name); | ||
| const intervalId = setInterval(() => { | ||
| func(...args); | ||
| }, delay); | ||
| this.intervals[name] = this.intervals[name] || []; | ||
| this.intervals[name].push(intervalId); | ||
| return intervalId; | ||
| } | ||
| if (typeof module !== 'undefined') { | ||
| module.exports = timerPro; | ||
| } | ||
| clearTimeout(name = null) { | ||
| if (name) { | ||
| if (this.timeouts[name]) { | ||
| this.timeouts[name].forEach(clearTimeout); | ||
| delete this.timeouts[name]; | ||
| } | ||
| } else { | ||
| Object.keys(this.timeouts).forEach((key) => { | ||
| this.timeouts[key].forEach(clearTimeout); | ||
| delete this.timeouts[key]; | ||
| }); | ||
| } | ||
| } | ||
| clearInterval(name = null) { | ||
| if (name) { | ||
| if (this.intervals[name]) { | ||
| this.intervals[name].forEach(clearInterval); | ||
| delete this.intervals[name]; | ||
| } | ||
| } else { | ||
| Object.keys(this.intervals).forEach((key) => { | ||
| this.intervals[key].forEach(clearInterval); | ||
| delete this.intervals[key]; | ||
| }); | ||
| } | ||
| } | ||
| clearAll() { | ||
| this.clearTimeout(); | ||
| this.clearInterval(); | ||
| } | ||
| } | ||
| // Expose the module for Node.js and the browser | ||
| if (typeof module !== 'undefined' && module.exports) { | ||
| module.exports = new TimerPro(); | ||
| } else { | ||
| global.timerPro = new TimerPro(); | ||
| } | ||
| })(typeof window !== 'undefined' ? window : global); |
@@ -1,1 +0,7 @@ | ||
| const timerPro={timers:{Timeout:{},Interval:{}},timerAll:function(){return timerPro.timers},timerExist:function(r,e){return timerPro.timers[r]&&timerPro.timers[r][e]},isArray:function(r){return Array.isArray(r)},new:{newTimer:function(r,e,t){timerPro.timerExist(r,e)||(timerPro.timers[r][e]=[]),timerPro.timers[r][e].push(t)},timeout:function(r,e,t,i=[],m=!1){timerPro.isArray(i)||(i=[]),m&&timerPro.reset.timeout(),timerPro.new.newTimer("Timeout",r,setTimeout(e,t,...i))},interval:function(r,e,t,i=[],m=!1){timerPro.isArray(i)||(i=[]),m&&timerPro.reset.interval(),timerPro.new.newTimer("Interval",r,setTimeout(e,t,...i))}},clear:{doClear:function(r,e){if(timerPro.timerExist(r,e)){for(timer of timerPro.timers[r][e])"Timeout"==r&&clearTimeout(timer),"Interval"==r&&clearInterval(timer);timerPro.timers[r][e]=[]}},timeout:function(r){timerPro.clear.doClear("Timeout",r)},interval:function(r){timerPro.clear.doClear("Interval",r)},timer(r){for(name in timerPro.timers[r])timerPro.clear.doClear(r,name)},all(){timerPro.clear.timer("Timeout"),timerPro.clear.timer("Interval")}},clean:{doClean:function(r,e){if(!timerPro.timerExist(r,e))return;let t,i=[];for(id in timerPro.timers[r][e])t=timerPro.timers[r][e][id],t&&!t._destroyed&&i.push(t);timerPro.timers[r][e]=i},timeout:function(r){timerPro.clean.doClean("Timeout",r)},interval:function(r){timerPro.clean.doClean("Interval",r)},timer:function(r){for(name in timerPro.timers[r])timerPro.clean.doClean(r,name)},all:function(){timerPro.clean.timer("Timeout"),timerPro.clean.timer("Interval")}},reset:{timeout:function(r){timerPro.clear.timeout(r),timerPro.clean.timeout(r)},interval:function(r){timerPro.clear.interval(r),timerPro.clean.interval(r)}}};"undefined"!=typeof module&&(module.exports=timerPro); | ||
| (function(global){class TimerPro{constructor(){this.timeouts={};this.intervals={}} | ||
| setTimeout(name,func,delay,args=[]){this.clearTimeout(name);const timeoutId=setTimeout(()=>{func(...args);delete this.timeouts[name]},delay);this.timeouts[name]=this.timeouts[name]||[];this.timeouts[name].push(timeoutId);return timeoutId} | ||
| setInterval(name,func,delay,args=[]){this.clearInterval(name);const intervalId=setInterval(()=>{func(...args)},delay);this.intervals[name]=this.intervals[name]||[];this.intervals[name].push(intervalId);return intervalId} | ||
| clearTimeout(name=null){if(name){if(this.timeouts[name]){this.timeouts[name].forEach(clearTimeout);delete this.timeouts[name]}}else{Object.keys(this.timeouts).forEach((key)=>{this.timeouts[key].forEach(clearTimeout);delete this.timeouts[key]})}} | ||
| clearInterval(name=null){if(name){if(this.intervals[name]){this.intervals[name].forEach(clearInterval);delete this.intervals[name]}}else{Object.keys(this.intervals).forEach((key)=>{this.intervals[key].forEach(clearInterval);delete this.intervals[key]})}} | ||
| clearAll(){this.clearTimeout();this.clearInterval()}} | ||
| if(typeof module!=='undefined'&&module.exports){module.exports=new TimerPro()}else{global.timerPro=new TimerPro()}})(typeof window!=='undefined'?window:global) |
+1
-1
| { | ||
| "name": "timerpro", | ||
| "version": "1.0.2", | ||
| "version": "1.0.3", | ||
| "description": "Manage intervals and timeouts like a pro", | ||
@@ -5,0 +5,0 @@ "main": "dist/timerpro.js", |
+97
-63
@@ -1,77 +0,111 @@ | ||
| Since intervals and timeouts lists are not saved on the browser. This package helps you to manage them. | ||
| # timerPro | ||
| --- | ||
| A lightweight JavaScript module for managing `setTimeout` and `setInterval` timers. This module works in both Node.js and the browser, providing an easy-to-use API for creating, clearing, and managing timers. | ||
| ## Add “TimerPro” to your project: | ||
| ## Installation | ||
| ### NodeJs | ||
| ```plaintext | ||
| const timerPro = require("timerpro"); | ||
| You can install `timerpro` via npm: | ||
| ```bash | ||
| npm install timerpro | ||
| ``` | ||
| ### Browser: | ||
| ```plaintext | ||
| <script src="https://cdn.jsdelivr.net/npm/timerpro/dist/timerpro.min.js"></script> | ||
| ``` | ||
| ## Methods: | ||
| ### Create new timeout (setTimeout) | ||
| - name: Timeout name | ||
| - func: Code to run | ||
| - delay: Run code after ms | ||
| - args: Array of arguments to pass inside the func | ||
| - reset: Auto reset before next run | ||
| Or simply include it in your project by adding the JavaScript file. | ||
| ## Usage | ||
| The `timerpro` module allows you to set and manage named `setTimeout` and `setInterval` timers. You can also clear, reset, and clear all timers using the provided methods. | ||
| ### Importing the Module | ||
| #### Node.js | ||
| ```javascript | ||
| timerPro.new.timeout(name, func, delay, args = [], reset = false); | ||
| const timerPro = require('timerpro'); | ||
| ``` | ||
| ### Example: | ||
| ```javascript | ||
| timerPro.new.timeout("MyTimeout", (i, msg) => { | ||
| console.log(`${i} - ${msg}`); | ||
| }, 5000, [1, "Hello World !"]); | ||
| #### Browser | ||
| Include the script in your HTML file: | ||
| ```html | ||
| <script src="https://cdn.jsdelivr.net/npm/timerpro/dist/timerpro.min.js"></script> | ||
| ``` | ||
| ### Create new interval (setInterval) | ||
| - name: Interval name | ||
| - func: Code to run | ||
| - delay: Run code after ms | ||
| - args: Array of arguments to pass inside the func | ||
| - reset: Auto reset before next run | ||
| Then, access it via `timerPro`: | ||
| ```javascript | ||
| timerPro.new.interval(name, func, delay, args = [], reset = false); | ||
| timerPro.setTimeout("example", () => console.log("Hello!"), 1000); | ||
| ``` | ||
| ### Example: | ||
| ### API Methods | ||
| #### `timerPro.setTimeout(name, func, delay, args = [])` | ||
| Creates a new `setTimeout` timer. | ||
| - **Parameters:** | ||
| - `name` (string): The timer name. | ||
| - `func` (function): The function to execute after the delay. | ||
| - `delay` (number): Time in milliseconds before the function is executed. | ||
| - `args` (array): Optional arguments to pass to the function. | ||
| - **Returns:** The ID of the created `setTimeout`. | ||
| #### `timerPro.setInterval(name, func, delay, args = [])` | ||
| Creates a new `setInterval` timer. | ||
| - **Parameters:** | ||
| - `name` (string): The timer name. | ||
| - `func` (function): The function to execute repeatedly. | ||
| - `delay` (number): Time in milliseconds between function executions. | ||
| - `args` (array): Optional arguments to pass to the function. | ||
| - **Returns:** The ID of the created `setInterval`. | ||
| #### `timerPro.clearTimeout(name)` | ||
| Clears a `setTimeout` timer by name. If `name` is not provided, all `setTimeout` timers will be cleared. | ||
| - **Parameters:** | ||
| - `name` (string, optional): The timer name. | ||
| #### `timerPro.clearInterval(name)` | ||
| Clears a `setInterval` timer by name. If `name` is not provided, all `setInterval` timers will be cleared. | ||
| - **Parameters:** | ||
| - `name` (string, optional): The timer name. | ||
| #### `timerPro.clearAll()` | ||
| Clears all `setTimeout` and `setInterval` timers. | ||
| ### Example | ||
| Here's an example demonstrating how to use `timerpro`: | ||
| ```javascript | ||
| timerPro.new.interval("MyInterval", (i, msg) => { | ||
| console.log(`${i} - ${msg}`); | ||
| }, 5000, [1, "Hello World !"]); | ||
| const timerPro = require('timerpro'); | ||
| // Set a timeout | ||
| timerPro.setTimeout("MyTimeout", (i, msg) => { | ||
| console.log(`${i} - ${msg}`); | ||
| }, 5000, [1, "Hello World!"]); | ||
| // Set an interval | ||
| timerPro.setInterval("MyInterval", (i, msg) => { | ||
| console.log(`${i} - ${msg}`); | ||
| }, 2000, [1, "Repeating Message"]); | ||
| // Clear the timeout | ||
| timerPro.clearTimeout("MyTimeout"); | ||
| // Clear the interval | ||
| timerPro.clearInterval("MyInterval"); | ||
| // Clear all timers | ||
| timerPro.clearAll(); | ||
| ``` | ||
| ### Get all timers | ||
| Get all the timers (timeout and interval) in a Object. | ||
| ```javascript | ||
| timerPro.timerAll(); | ||
| ``` | ||
| ### Example: | ||
| ```javascript | ||
| console.log(timerPro.timerAll()); | ||
| ``` | ||
| ### Clear timers | ||
| Clear timers by name. Use all to clear all the timers. | ||
| ```javascript | ||
| timerPro.clear.timeout(name) | ||
| timerPro.clear.interval(name) | ||
| timerPro.clear.all() | ||
| ``` | ||
| ### Clean timers | ||
| Clean and reorder finished timers by name. Use all to clean and reorder all the timers. | ||
| ```javascript | ||
| timerPro.clean.timeout(name) | ||
| timerPro.clean.interval(name) | ||
| timerPro.clean.all() | ||
| ``` | ||
| ### Reset timers | ||
| Automatically clear and clean the timers. | ||
| ```javascript | ||
| timerPro.reset.timeout(name) | ||
| timerPro.reset.interval(name) | ||
| ``` |
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.
Found 1 instance in 1 package
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.
Found 1 instance in 1 package
6760
1.59%2
-33.33%112
45.45%0
-100%69
-27.37%