![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
safe-timers
Advanced tools
Q: What's this all about? Aren't JavaScript timers safe? A: Long story short: they're a bit broken. This module unbreaks them.
Whether it's by spec, or by accident, all major browsers and Node.js limit the interval a setTimeout can accept to a 32 bit signed integer. What that means in essence is that a timeout can never last longer than 24.85 days. Long enough, right?
The problem is that:
All the arguments about "you shouldn't need intervals this big anyway" go out the window the moment you provide a big one and instead of never firing, it fires immediately. This is a real problem. And so here we are, Safe Timers solves this for you.
Does that mean you should forego the browser native setTimeout and setInterval altogether? Absolutely not. Most of the time, we pass constant short intervals, in which case Safe Timers are overkill. But when your interval comes from some variable that depends on state or user input, using Safe Timers is a good idea.
Timer setTimeout(Function callback, number interval, mixed arg1, mixed arg2, ...)
Calls callback
after at least interval
milliseconds have passed. All arguments passed after the interval
will be
passed to the callback once it gets invoked. Returns a Timer
instance.
const setTimeout = require('safe-timers').setTimeout;
setTimeout(function (msg) {
console.log(msg);
}, 5000, 'Hello world');
Timer setTimeoutAt(Function callback, number timestamp, mixed arg1, mixed arg2, ...)
Calls callback
when our clock reaches the given timestamp
(in milliseconds). All arguments passed after the
interval
will be passed to the callback once it gets invoked. Returns a Timer
instance.
const setTimeoutAt = require('safe-timers').setTimeoutAt;
setTimeoutAt(function (msg) {
console.log(msg);
}, Date.now() + 5000, 'Hello world');
Interval setInterval(Function callback, number interval, mixed arg1, mixed arg2, ...)
Calls callback
after at least every interval
milliseconds. All arguments passed after the interval
will be passed
to the callback when it gets invoked. Returns an Interval
instance.
const setInterval = require('safe-timers').setInterval;
setInterval(function (msg) {
console.log(msg);
}, 5000, 'Hello world');
timer.clear() / interval.clear()
The response from safetimers.setTimeout[At]
and safetimers.setInterval
are Timer
and Interval
objects
respectively. To cancel a timer or interval, you can call clear
on it.
const setTimeout = require('safe-timers').setTimeout;
const timer = setTimeout(function (msg) {
console.log(msg); // this will never show
}, 5000, 'Hello world');
timer.clear();
FAQs
Timers with near-infinite duration support
The npm package safe-timers receives a total of 38,679 weekly downloads. As such, safe-timers popularity was classified as popular.
We found that safe-timers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.