node-on-idle

Trigger an onIdle event on nodejs application

Usage no npm install needed!

<script type="module">
  import nodeOnIdle from 'https://cdn.skypack.dev/node-on-idle';
</script>

README

on-idle

Build Status JavaScript Style Guide

Trigger an idle event when the Node.js process is in the idle status!

You need Node.js >= 12.9.0 to run this module since perf_hooks are used under the hood.

Usage

The idle event is fired no more than 1 time every throttlingms and only when idleLimitms has been expired. The check of the limit is performed every sampleIntervalms.

const onIdle = require('node-on-idle')

const eventEmitter = onIdle()
eventEmitter.on('idle', function (idleMs) {
  // do some stuff when the process is in idle
})

// You can set the options like this:
const eventEmitter = onIdle({
  idleLimit: 2000, // default value
  throttling: 10000, // default value
  sampleInterval: 1000 // default value
})

The sampling will start when the first listener will be appended to the EventEmitter returned.

License

Licensed under MIT.