timerpro
v1.0.4
Published
Manage intervals and timeouts like a pro
Downloads
14
Readme
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.
Installation
You can install timerpro
via npm:
npm install timerpro
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
const timerPro = require('timerpro');
Browser
Include the script in your HTML file:
<script src="https://cdn.jsdelivr.net/npm/timerpro/dist/timerpro.min.js"></script>
Then, access it via timerPro
:
timerPro.setTimeout("example", () => console.log("Hello!"), 1000);
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
:
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();