The Named Timers JavaScript plugin adds a powerful wrapper in the form of a timer abstraction around Node.js's or the web browser's setInterval function to allow developers to work more easily with intervals in the form of timers, which can be referred to conveniently by name, rather than ID.
The named timers can also be invoked in the context of a jQuery selection, allowing their code to conveniently access the jQuery object containing that selection using this. A full examples page detailing usage of the plugin can be found here, and the various functions provided by the plugin are also documented via comments in the code itself, so that's worth a look for reference purposes.
Note that you can also use this plugin in a Node.js context with:
const namedTimers = require("named-timers"); namedTimers().createTimer("regularTimer").startTimer(5000, function() { // do some work... // ... } ); namedTimers("regularTimer").stopTimer(); namedTimers().createTimer("oneOffTimer").startTimer(5000, function() { // remove timer to make this a one-off namedTimers().deleteTimer("oneOffTimer"); // do some work... // ... } );
You can also just work with the NamedTimerCollection returned by the named-timers module export when called with no parameters:
const namedTimers = require("named-timers")(); namedTimers.getTimer("regularTimer").setTimerCallback(function() { // do different work... // ... });
If using Browserify, you can also extend an existing jQuery object with this plugin by passing it in instead of a timer name:
var $ = require("jquery"); require("named-timers")($); $('#someElement').namedTimers().createTimer("timer123").startTimer(...
There is an NPM page for this plugin, and it's recommended that you use NPM to install the plugin and keep it up-to-date:
npm install named-timers
Also note the project's Git repository on Gitlab - you can use it to check out the source code, provide improvements, report bugs, or request enhancements. The Named Timers JavaScript plugin is distributed under the GNU General Public License, V3.