├── .github └── workflows │ └── test.yml ├── .gitignore ├── .husky ├── commit-msg └── pre-commit ├── LICENSE ├── README.md ├── config ├── eslint │ ├── config.json │ ├── src.json │ └── test.json ├── karma │ └── config-unit.js ├── lint-staged │ └── config.json ├── prettier │ └── config.json ├── rollup │ └── bundle.mjs └── tslint │ └── src.json ├── package-lock.json ├── package.json ├── src ├── module.ts ├── tsconfig.json └── types │ ├── function-map.ts │ ├── index.ts │ └── timer-type.ts └── test └── unit └── module.js /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | 15 | strategy: 16 | matrix: 17 | node-version: [20.x] 18 | target: [chrome, firefox, safari] 19 | type: [unit] 20 | max-parallel: 3 21 | 22 | steps: 23 | - name: Checkout repository 24 | uses: actions/checkout@v4 25 | 26 | - name: Install Node.js ${{ matrix.node-version }} 27 | uses: actions/setup-node@v4 28 | with: 29 | node-version: ${{ matrix.node-version }} 30 | 31 | - name: Cache node modules 32 | uses: actions/cache@v4 33 | with: 34 | path: ~/.npm 35 | key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} 36 | restore-keys: | 37 | ${{ runner.os }}-node- 38 | 39 | - name: Install dependencies 40 | run: npm ci 41 | 42 | - env: 43 | BROWSER_STACK_ACCESS_KEY: ${{ secrets.BROWSER_STACK_ACCESS_KEY }} 44 | BROWSER_STACK_USERNAME: ${{ secrets.BROWSER_STACK_USERNAME }} 45 | TARGET: ${{ matrix.target }} 46 | TYPE: ${{ matrix.type }} 47 | name: Run ${{ matrix.type }} tests 48 | run: npm test 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | node_modules/ 3 | /build/ 4 | -------------------------------------------------------------------------------- /.husky/commit-msg: -------------------------------------------------------------------------------- 1 | commitlint --edit --extends @commitlint/config-angular 2 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | lint-staged --config config/lint-staged/config.json && npm run lint 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Christoph Guttandin 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 | # audio-context-timers 2 | 3 | **A replacement for setInterval() and setTimeout() which works in unfocused windows.** 4 | 5 | [![version](https://img.shields.io/npm/v/audio-context-timers.svg?style=flat-square)](https://www.npmjs.com/package/audio-context-timers) 6 | 7 | ## Motivation 8 | 9 | For scripts that rely on [WindowTimers](http://www.w3.org/TR/html5/webappapis.html#timers) 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 at which they invoke those timers to a maximum of once per second in such a situation. However it is possible to schedule [AudioBufferSourceNodes](https://webaudio.github.io/web-audio-api/#AudioBufferSourceNode) and listen for their [`onended`](https://webaudio.github.io/web-audio-api/#widl-AudioScheduledSourceNode-onended) event to achieve a similar result. This makes it possible to avoid the throttling. 10 | 11 | ## Getting Started 12 | 13 | `audio-context-timers` is available as a package on [npm](https://www.npmjs.org/package/audio-context-timers). Run the following command to install it: 14 | 15 | ```shell 16 | npm install audio-context-timers 17 | ``` 18 | 19 | You can then import the exported functions in your code like this: 20 | 21 | ```js 22 | import { clearInterval, clearTimeout, setInterval, setTimeout } from 'audio-context-timers'; 23 | ``` 24 | 25 | The usage is exactly the same as with the corresponding functions on the global scope. 26 | 27 | ```js 28 | var intervalId = setInterval(() => { 29 | // do something many times 30 | }, 100); 31 | 32 | clearInterval(intervalId); 33 | 34 | var timeoutId = setTimeout(() => { 35 | // do something once 36 | }, 100); 37 | 38 | clearTimeout(timeoutId); 39 | ``` 40 | 41 | However there are some subtle differences between `audio-context-timers` and WindowTimers which are the same those of the [`worker-timers`](https://github.com/chrisguttandin/worker-timers) package. 42 | -------------------------------------------------------------------------------- /config/eslint/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true 4 | }, 5 | "extends": "eslint-config-holy-grail", 6 | "rules": { 7 | "no-sync": "off", 8 | "node/no-missing-require": "off" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /config/eslint/src.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true 4 | }, 5 | "extends": "eslint-config-holy-grail" 6 | } 7 | -------------------------------------------------------------------------------- /config/eslint/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "mocha": true 5 | }, 6 | "extends": "eslint-config-holy-grail", 7 | "globals": { 8 | "expect": "readonly" 9 | }, 10 | "rules": { 11 | "no-unused-expressions": "off", 12 | "node/file-extension-in-import": "off", 13 | "node/no-missing-require": "off" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /config/karma/config-unit.js: -------------------------------------------------------------------------------- 1 | const { env } = require('process'); 2 | const { DefinePlugin } = require('webpack'); 3 | 4 | module.exports = (config) => { 5 | config.set({ 6 | basePath: '../../', 7 | 8 | browserDisconnectTimeout: 100000, 9 | 10 | browserNoActivityTimeout: 100000, 11 | 12 | client: { 13 | mocha: { 14 | bail: true, 15 | timeout: 20000 16 | } 17 | }, 18 | 19 | concurrency: 1, 20 | 21 | files: [ 22 | { 23 | included: false, 24 | pattern: 'src/**', 25 | served: false, 26 | watched: true 27 | }, 28 | 'test/unit/**/*.js' 29 | ], 30 | 31 | frameworks: ['mocha', 'sinon-chai'], 32 | 33 | preprocessors: { 34 | 'test/unit/**/*.js': 'webpack' 35 | }, 36 | 37 | reporters: ['dots'], 38 | 39 | webpack: { 40 | mode: 'development', 41 | module: { 42 | rules: [ 43 | { 44 | test: /\.ts?$/, 45 | use: { 46 | loader: 'ts-loader', 47 | options: { 48 | compilerOptions: { 49 | declaration: false, 50 | declarationMap: false 51 | } 52 | } 53 | } 54 | } 55 | ] 56 | }, 57 | plugins: [ 58 | new DefinePlugin({ 59 | 'process.env': { 60 | CI: JSON.stringify(env.CI) 61 | } 62 | }) 63 | ], 64 | resolve: { 65 | extensions: ['.js', '.ts'], 66 | fallback: { util: false } 67 | } 68 | }, 69 | 70 | webpackMiddleware: { 71 | noInfo: true 72 | } 73 | }); 74 | 75 | if (env.CI) { 76 | config.set({ 77 | browserStack: { 78 | accessKey: env.BROWSER_STACK_ACCESS_KEY, 79 | build: `${env.GITHUB_RUN_ID}/unit-${env.TARGET}`, 80 | forceLocal: true, 81 | localIdentifier: `${Math.floor(Math.random() * 1000000)}`, 82 | project: env.GITHUB_REPOSITORY, 83 | username: env.BROWSER_STACK_USERNAME, 84 | video: false 85 | }, 86 | 87 | browsers: 88 | env.TARGET === 'chrome' 89 | ? ['ChromeBrowserStack'] 90 | : env.TARGET === 'firefox' 91 | ? ['FirefoxBrowserStack'] 92 | : env.TARGET === 'safari' 93 | ? ['SafariBrowserStack'] 94 | : ['ChromeBrowserStack', 'FirefoxBrowserStack', 'SafariBrowserStack'], 95 | 96 | captureTimeout: 300000, 97 | 98 | customLaunchers: { 99 | ChromeBrowserStack: { 100 | base: 'BrowserStack', 101 | browser: 'chrome', 102 | captureTimeout: 300, 103 | os: 'OS X', 104 | os_version: 'Big Sur' // eslint-disable-line camelcase 105 | }, 106 | FirefoxBrowserStack: { 107 | base: 'BrowserStack', 108 | browser: 'firefox', 109 | captureTimeout: 300, 110 | os: 'Windows', 111 | os_version: '10' // eslint-disable-line camelcase 112 | }, 113 | SafariBrowserStack: { 114 | base: 'BrowserStack', 115 | browser: 'safari', 116 | captureTimeout: 300, 117 | os: 'OS X', 118 | os_version: 'Big Sur' // eslint-disable-line camelcase 119 | } 120 | } 121 | }); 122 | } else { 123 | config.set({ 124 | browsers: [ 125 | 'ChromeCanaryHeadlessWithFlags', 126 | 'ChromeHeadlessWithFlags', 127 | 'FirefoxDeveloperWithPrefs', 128 | 'FirefoxWithPrefs', 129 | 'Safari' 130 | ], 131 | 132 | customLaunchers: { 133 | ChromeCanaryHeadlessWithFlags: { 134 | base: 'ChromeCanaryHeadless', 135 | flags: ['--autoplay-policy=no-user-gesture-required'] 136 | }, 137 | ChromeHeadlessWithFlags: { 138 | base: 'ChromeHeadless', 139 | flags: ['--autoplay-policy=no-user-gesture-required'] 140 | }, 141 | FirefoxDeveloperWithPrefs: { 142 | base: 'FirefoxDeveloperHeadless', 143 | prefs: { 144 | 'media.autoplay.default': 0 145 | } 146 | }, 147 | FirefoxWithPrefs: { 148 | base: 'FirefoxHeadless', 149 | prefs: { 150 | 'media.autoplay.default': 0 151 | } 152 | } 153 | } 154 | }); 155 | } 156 | }; 157 | -------------------------------------------------------------------------------- /config/lint-staged/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "*": "prettier --config config/prettier/config.json --ignore-unknown --write" 3 | } 4 | -------------------------------------------------------------------------------- /config/prettier/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "arrowParens": "always", 3 | "printWidth": 140, 4 | "quoteProps": "consistent", 5 | "singleQuote": true, 6 | "tabWidth": 4, 7 | "trailingComma": "none" 8 | } 9 | -------------------------------------------------------------------------------- /config/rollup/bundle.mjs: -------------------------------------------------------------------------------- 1 | import babel from '@rollup/plugin-babel'; 2 | 3 | // eslint-disable-next-line import/no-default-export 4 | export default { 5 | input: 'build/es2019/module.js', 6 | output: { 7 | file: 'build/es5/bundle.js', 8 | format: 'umd', 9 | name: 'audioContextTimers' 10 | }, 11 | plugins: [ 12 | babel({ 13 | babelHelpers: 'runtime', 14 | exclude: 'node_modules/**', 15 | plugins: ['@babel/plugin-external-helpers', '@babel/plugin-transform-runtime'], 16 | presets: [ 17 | [ 18 | '@babel/preset-env', 19 | { 20 | modules: false 21 | } 22 | ] 23 | ] 24 | }) 25 | ] 26 | }; 27 | -------------------------------------------------------------------------------- /config/tslint/src.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint-config-holy-grail" 3 | } 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Christoph Guttandin", 3 | "bugs": { 4 | "url": "https://github.com/chrisguttandin/audio-context-timers/issues" 5 | }, 6 | "config": { 7 | "commitizen": { 8 | "path": "cz-conventional-changelog" 9 | } 10 | }, 11 | "dependencies": { 12 | "@babel/runtime": "^7.27.1", 13 | "fast-unique-numbers": "^9.0.21", 14 | "standardized-audio-context": "^25.3.77", 15 | "tslib": "^2.8.1" 16 | }, 17 | "description": "A replacement for setInterval() and setTimeout() which works in unfocused windows.", 18 | "devDependencies": { 19 | "@babel/core": "^7.27.1", 20 | "@babel/plugin-external-helpers": "^7.27.1", 21 | "@babel/plugin-transform-runtime": "^7.27.1", 22 | "@babel/preset-env": "^7.27.2", 23 | "@commitlint/cli": "^19.8.1", 24 | "@commitlint/config-angular": "^19.8.1", 25 | "@rollup/plugin-babel": "^6.0.4", 26 | "chai": "^4.3.10", 27 | "commitizen": "^4.3.1", 28 | "cz-conventional-changelog": "^3.3.0", 29 | "eslint": "^8.57.0", 30 | "eslint-config-holy-grail": "^60.0.34", 31 | "husky": "^9.1.7", 32 | "karma": "^6.4.4", 33 | "karma-browserstack-launcher": "^1.6.0", 34 | "karma-chrome-launcher": "^3.2.0", 35 | "karma-firefox-launcher": "^2.1.3", 36 | "karma-mocha": "^2.0.1", 37 | "karma-sinon-chai": "^2.0.2", 38 | "karma-webkit-launcher": "^2.6.0", 39 | "karma-webpack": "^5.0.1", 40 | "lint-staged": "^15.5.0", 41 | "mocha": "^11.2.2", 42 | "prettier": "^3.5.3", 43 | "rimraf": "^6.0.1", 44 | "rollup": "^4.40.2", 45 | "sinon": "^17.0.2", 46 | "sinon-chai": "^3.7.0", 47 | "ts-loader": "^9.5.2", 48 | "tsconfig-holy-grail": "^15.0.2", 49 | "tslint": "^6.1.3", 50 | "tslint-config-holy-grail": "^56.0.6", 51 | "typescript": "^5.8.3", 52 | "webpack": "^5.99.8" 53 | }, 54 | "files": [ 55 | "build/es2019/", 56 | "build/es5/", 57 | "src/" 58 | ], 59 | "homepage": "https://github.com/chrisguttandin/audio-context-timers", 60 | "keywords": [ 61 | "Web Audio API", 62 | "WindowTimers", 63 | "clearInterval", 64 | "clearTimeout", 65 | "interval", 66 | "setInterval", 67 | "setTimeout" 68 | ], 69 | "license": "MIT", 70 | "main": "build/es5/bundle.js", 71 | "module": "build/es2019/module.js", 72 | "name": "audio-context-timers", 73 | "repository": { 74 | "type": "git", 75 | "url": "https://github.com/chrisguttandin/audio-context-timers.git" 76 | }, 77 | "scripts": { 78 | "build": "rimraf build/* && tsc --project src/tsconfig.json && rollup --config config/rollup/bundle.mjs", 79 | "lint": "npm run lint:config && npm run lint:src && npm run lint:test", 80 | "lint:config": "eslint --config config/eslint/config.json --ext .cjs --ext .js --ext .mjs --report-unused-disable-directives config/", 81 | "lint:src": "tslint --config config/tslint/src.json --project src/tsconfig.json src/*.ts src/**/*.ts", 82 | "lint:test": "eslint --config config/eslint/test.json --ext .js --report-unused-disable-directives test/", 83 | "prepare": "husky", 84 | "prepublishOnly": "npm run build", 85 | "test": "npm run lint && npm run build && npm run test:unit", 86 | "test:unit": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"unit\" ]; then karma start config/karma/config-unit.js --single-run; fi" 87 | }, 88 | "types": "build/es2019/module.d.ts", 89 | "version": "5.0.129" 90 | } 91 | -------------------------------------------------------------------------------- /src/module.ts: -------------------------------------------------------------------------------- 1 | import { generateUniqueNumber } from 'fast-unique-numbers'; 2 | import { AudioBuffer, AudioBufferSourceNode, MinimalAudioContext, isSupported } from 'standardized-audio-context'; 3 | import { TFunctionMap, TTimerType } from './types'; 4 | 5 | const MINIMAL_AUDIO_CONTEXT = new MinimalAudioContext(); 6 | 7 | const AUDIO_BUFFER = new AudioBuffer({ length: 2, sampleRate: MINIMAL_AUDIO_CONTEXT.sampleRate }); 8 | 9 | const SAMPLE_DURATION = 2 / MINIMAL_AUDIO_CONTEXT.sampleRate; 10 | 11 | const SCHEDULED_TIMEOUT_FUNCTIONS: TFunctionMap = new Map(); 12 | 13 | const SCHEDULED_INTERVAL_FUNCTIONS: TFunctionMap = new Map(); 14 | 15 | const callIntervalFunction = (id: number, type: TTimerType) => { 16 | const functions = type === 'interval' ? SCHEDULED_INTERVAL_FUNCTIONS : SCHEDULED_TIMEOUT_FUNCTIONS; 17 | 18 | if (functions.has(id)) { 19 | const func = functions.get(id); 20 | 21 | if (func !== undefined) { 22 | func(); 23 | 24 | if (type === 'timeout') { 25 | SCHEDULED_TIMEOUT_FUNCTIONS.delete(id); 26 | } 27 | } 28 | } 29 | }; 30 | 31 | const scheduleFunction = (id: number, delay: number, type: TTimerType) => { 32 | const now = performance.now(); 33 | 34 | const audioBufferSourceNode = new AudioBufferSourceNode(MINIMAL_AUDIO_CONTEXT, { buffer: AUDIO_BUFFER }); 35 | 36 | audioBufferSourceNode.onended = () => { 37 | const elapsedTime = performance.now() - now; 38 | 39 | if (elapsedTime >= delay) { 40 | callIntervalFunction(id, type); 41 | } else { 42 | scheduleFunction(id, delay - elapsedTime, type); 43 | } 44 | 45 | audioBufferSourceNode.disconnect(MINIMAL_AUDIO_CONTEXT.destination); 46 | }; 47 | audioBufferSourceNode.connect(MINIMAL_AUDIO_CONTEXT.destination); 48 | audioBufferSourceNode.start(Math.max(0, MINIMAL_AUDIO_CONTEXT.currentTime + delay / 1000 - SAMPLE_DURATION)); 49 | }; 50 | 51 | export const clearInterval = (id: number) => { 52 | SCHEDULED_INTERVAL_FUNCTIONS.delete(id); 53 | }; 54 | 55 | export const clearTimeout = (id: number) => { 56 | SCHEDULED_TIMEOUT_FUNCTIONS.delete(id); 57 | }; 58 | 59 | export { isSupported }; 60 | 61 | export const setInterval = (func: Function, delay: number) => { 62 | const id = generateUniqueNumber(SCHEDULED_INTERVAL_FUNCTIONS); 63 | 64 | SCHEDULED_INTERVAL_FUNCTIONS.set(id, () => { 65 | func(); 66 | 67 | scheduleFunction(id, delay, 'interval'); 68 | }); 69 | 70 | scheduleFunction(id, delay, 'interval'); 71 | 72 | return id; 73 | }; 74 | 75 | export const setTimeout = (func: Function, delay: number) => { 76 | const id = generateUniqueNumber(SCHEDULED_TIMEOUT_FUNCTIONS); 77 | 78 | SCHEDULED_TIMEOUT_FUNCTIONS.set(id, func); 79 | 80 | scheduleFunction(id, delay, 'timeout'); 81 | 82 | return id; 83 | }; 84 | -------------------------------------------------------------------------------- /src/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "isolatedModules": true 4 | }, 5 | "extends": "tsconfig-holy-grail/src/tsconfig-browser" 6 | } 7 | -------------------------------------------------------------------------------- /src/types/function-map.ts: -------------------------------------------------------------------------------- 1 | export type TFunctionMap = Map; 2 | -------------------------------------------------------------------------------- /src/types/index.ts: -------------------------------------------------------------------------------- 1 | export * from './function-map'; 2 | export * from './timer-type'; 3 | -------------------------------------------------------------------------------- /src/types/timer-type.ts: -------------------------------------------------------------------------------- 1 | export type TTimerType = 'interval' | 'timeout'; 2 | -------------------------------------------------------------------------------- /test/unit/module.js: -------------------------------------------------------------------------------- 1 | import * as audioContextTimers from '../../src/module'; 2 | 3 | describe('module', () => { 4 | describe('clearInterval()', () => { 5 | it('should not call the function after clearing the interval', (done) => { 6 | const id = audioContextTimers.setInterval(() => { 7 | throw new Error('this should never be called'); 8 | }, 100); 9 | 10 | audioContextTimers.clearInterval(id); 11 | 12 | // Wait 200ms to be sure the function never gets called. 13 | setTimeout(done, 200); 14 | }); 15 | 16 | it('should not call the function anymore after clearing the interval after the first callback', (done) => { 17 | let id = audioContextTimers.setInterval(() => { 18 | if (id === null) { 19 | throw new Error('this should never be called'); 20 | } 21 | 22 | audioContextTimers.clearInterval(id); 23 | id = null; 24 | }, 50); 25 | 26 | // Wait 200ms to be sure the function gets not called anymore. 27 | setTimeout(done, 200); 28 | }); 29 | }); 30 | 31 | describe('clearTimeout()', () => { 32 | it('should not call the function after clearing the timeout', (done) => { 33 | const id = audioContextTimers.setTimeout(() => { 34 | throw new Error('this should never be called'); 35 | }, 100); 36 | 37 | audioContextTimers.clearTimeout(id); 38 | 39 | // Wait 200ms to be sure the function never gets called. 40 | setTimeout(done, 200); 41 | }); 42 | }); 43 | 44 | describe('setInterval()', () => { 45 | let id; 46 | 47 | afterEach(() => { 48 | audioContextTimers.clearTimeout(id); 49 | }); 50 | 51 | it('should return a numeric id', () => { 52 | id = audioContextTimers.setInterval(() => {}, 0); 53 | 54 | expect(id).to.be.a('number'); 55 | }); 56 | 57 | // @todo There is currently no way to disable the autoplay policy on BrowserStack or Sauce Labs. 58 | // eslint-disable-next-line no-undef 59 | if (!process.env.CI) { 60 | it('should constantly call a function with the given delay', function (done) { 61 | this.timeout(4000); 62 | 63 | let before = performance.now(); 64 | let calls = 0; 65 | 66 | function func() { 67 | const now = performance.now(); 68 | const elapsed = now - before; 69 | 70 | expect(elapsed).to.be.at.least(100); 71 | 72 | // Test five calls. 73 | if (calls > 4) { 74 | done(); 75 | } 76 | 77 | before = now; 78 | calls += 1; 79 | } 80 | 81 | id = audioContextTimers.setInterval(func, 100); 82 | }); 83 | } 84 | }); 85 | 86 | describe('setTimeout()', () => { 87 | let id; 88 | 89 | afterEach(() => { 90 | audioContextTimers.clearInterval(id); 91 | }); 92 | 93 | it('should return a numeric id', () => { 94 | id = audioContextTimers.setTimeout(() => {}, 0); 95 | 96 | expect(id).to.be.a('number'); 97 | }); 98 | 99 | // @todo There is currently no way to disable the autoplay policy on BrowserStack or Sauce Labs. 100 | // eslint-disable-next-line no-undef 101 | if (!process.env.CI) { 102 | it('should postpone a function for the given delay', (done) => { 103 | const before = performance.now(); 104 | 105 | function func() { 106 | const elapsed = performance.now() - before; 107 | 108 | expect(elapsed).to.be.at.least(100); 109 | 110 | done(); 111 | } 112 | 113 | id = audioContextTimers.setTimeout(func, 100); 114 | }); 115 | } 116 | }); 117 | }); 118 | --------------------------------------------------------------------------------