Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
worker-timers
Advanced tools
A replacement for setInterval() and setTimeout() which works in unfocused windows
The worker-timers npm package provides timer functions (setTimeout, setInterval, and clearTimeout) that run in Web Workers. This allows for more accurate timing and less interference from the main thread, which can be beneficial for performance-sensitive applications.
setTimeout
The setTimeout function schedules a function to be executed after a specified delay, in milliseconds. This is similar to the native setTimeout but runs in a Web Worker for more accurate timing.
const { setTimeout } = require('worker-timers');
setTimeout(() => {
console.log('This runs after 1000ms');
}, 1000);
setInterval
The setInterval function repeatedly calls a function with a fixed time delay between each call. This is similar to the native setInterval but runs in a Web Worker for more consistent intervals.
const { setInterval } = require('worker-timers');
setInterval(() => {
console.log('This runs every 1000ms');
}, 1000);
clearTimeout
The clearTimeout function cancels a timeout previously established by calling setTimeout. This is similar to the native clearTimeout but works with the worker-timers setTimeout.
const { setTimeout, clearTimeout } = require('worker-timers');
const timeoutId = setTimeout(() => {
console.log('This will not run');
}, 1000);
clearTimeout(timeoutId);
clearInterval
The clearInterval function cancels a repeated action which was established by a call to setInterval. This is similar to the native clearInterval but works with the worker-timers setInterval.
const { setInterval, clearInterval } = require('worker-timers');
const intervalId = setInterval(() => {
console.log('This will not run');
}, 1000);
clearInterval(intervalId);
The worker-timer package provides similar functionality by running timer functions in Web Workers. It aims to provide more accurate timing and less interference from the main thread, similar to worker-timers.
A replacement for setInterval() and setTimeout() which works in unfocused windows.
For scripts that rely on WindowTimers like setInterval() or setTimeout() things get confusing when the site which the script is running on loses focus. Chrome, Firefox and maybe others throttle the frequency of firing those timers to a maximum of once per second in such a situation. However this is only true for the main thread and does not affect the behavior of Web Workers. Therefore it is possible to avoid the throttling by using a worker to do the actual scheduling. This is exactly what WorkerTimers do.
WorkerTimers are available as a package on npm. Simply run the following command to install it:
npm install worker-timers
You can then require the workerTimers instance from within your code like this:
var workerTimers = require('worker-timers');
The usage is exactly the same as with the corresponding functions on the global scope.
var intervalId = workerTimers.setInterval(function () {
// do something many times
}, 100);
workerTimers.clearInterval(intervalId);
var timeoutId = workerTimers.setTimeout(function () {
// do something once
}, 100);
workerTimers.clearTimeout(timeoutId);
FAQs
A replacement for setInterval() and setTimeout() which works in unfocused windows.
The npm package worker-timers receives a total of 122,751 weekly downloads. As such, worker-timers popularity was classified as popular.
We found that worker-timers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.