├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── example.js
├── index.js
├── package.json
└── screenshot.png
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | coverage/
3 | tmp/
4 | dist/
5 | npm-debug.log*
6 | .DS_Store
7 | .nyc_output
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | node_js:
2 | - "4"
3 | - "5"
4 | - "6"
5 | - "7"
6 | sudo: false
7 | language: node_js
8 | script: "npm run test"
9 | # after_success: "npm i -g codecov && npm run coverage && codecov"
10 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Yoshua Wuyts
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # on-idle [![stability][0]][1]
2 | [![npm version][2]][3] [![build status][4]][5]
3 | [![downloads][8]][9] [![js-standard-style][10]][11]
4 |
5 | Safely detect when the browser is idle. Does nothing when run in Node.
6 |
7 | 
8 |
9 | ## Usage
10 | ```js
11 | var onIdle = require('on-idle')
12 | var html = require('bel')
13 |
14 | var cancel = onIdle(function () {
15 | var el = html`
browser is idle
`
16 | document.body.appendChild(el)
17 | })
18 |
19 | if (somethingHappens) {
20 | cancel()
21 | }
22 | ```
23 |
24 | ## API
25 | ### `onIdle(callback, options)`
26 | Call a function when the browser has spare time. Calls it on the next frame if
27 | `window.requestIdleCallback` is not available. Does nothing in Node.
28 |
29 | ## License
30 | [MIT](https://tldrlegal.com/license/mit-license)
31 |
32 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
33 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index
34 | [2]: https://img.shields.io/npm/v/on-idle.svg?style=flat-square
35 | [3]: https://npmjs.org/package/on-idle
36 | [4]: https://img.shields.io/travis/choojs/on-idle/master.svg?style=flat-square
37 | [5]: https://travis-ci.org/choojs/on-idle
38 | [6]: https://img.shields.io/codecov/c/github/choojs/on-idle/master.svg?style=flat-square
39 | [7]: https://codecov.io/github/choojs/on-idle
40 | [8]: http://img.shields.io/npm/dm/on-idle.svg?style=flat-square
41 | [9]: https://npmjs.org/package/on-idle
42 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
43 | [11]: https://github.com/feross/standard
44 |
--------------------------------------------------------------------------------
/example.js:
--------------------------------------------------------------------------------
1 | var onIdle = require('./')
2 |
3 | window.localStorage.logLevel = 'debug'
4 |
5 | onIdle(function () {
6 | var i = 100
7 | while (--i) console.log('slow message')
8 | })
9 |
10 | onIdle(function () {
11 | console.log('fast message')
12 | })
13 |
14 | var cancel = onIdle(function () {
15 | console.log('this callback will be cancelled')
16 | })
17 |
18 | cancel()
19 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var assert = require('assert')
2 |
3 | var dftOpts = {}
4 | var hasWindow = typeof window !== 'undefined'
5 | var hasIdle = hasWindow && window.requestIdleCallback
6 |
7 | module.exports = onIdle
8 |
9 | function onIdle (cb, opts) {
10 | opts = opts || dftOpts
11 | var timerId
12 |
13 | assert.equal(typeof cb, 'function', 'on-idle: cb should be type function')
14 | assert.equal(typeof opts, 'object', 'on-idle: opts should be type object')
15 |
16 | if (hasIdle) {
17 | timerId = window.requestIdleCallback(function (idleDeadline) {
18 | // reschedule if there's less than 1ms remaining on the tick
19 | // and a timer did not expire
20 | if (idleDeadline.timeRemaining() <= 1 && !idleDeadline.didTimeout) {
21 | return onIdle(cb, opts)
22 | } else {
23 | cb(idleDeadline)
24 | }
25 | }, opts)
26 | return window.cancelIdleCallback.bind(window, timerId)
27 | } else if (hasWindow) {
28 | timerId = setTimeout(cb, 0)
29 | return clearTimeout.bind(window, timerId)
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "on-idle",
3 | "description": "Detect when the browser is idle",
4 | "repository": "choojs/on-idle",
5 | "version": "3.1.4",
6 | "scripts": {
7 | "deps": "dependency-check . && dependency-check . --extra --no-dev -i nanoassert",
8 | "start": "bankai start example.js",
9 | "test": "standard && npm run deps",
10 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov"
11 | },
12 | "browser": {
13 | "assert": "nanoassert"
14 | },
15 | "dependencies": {
16 | "nanoassert": "^1.1.0"
17 | },
18 | "devDependencies": {
19 | "bankai": "^7.6.2",
20 | "dependency-check": "^2.8.0",
21 | "nyc": "^10.2.0",
22 | "standard": "^9.0.2"
23 | },
24 | "keywords": [
25 | "browser",
26 | "idle",
27 | "requestIdleCallback"
28 | ]
29 | }
30 |
--------------------------------------------------------------------------------
/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/choojs/on-idle/b569840c877974e2c20c8db8773c894e47c427f0/screenshot.png
--------------------------------------------------------------------------------