{"id":18860,"library":"timer-shim","title":"timer-shim","description":"Timer-shim is a lightweight wrapper around setTimeout, setInterval, and clearTimeout/clearInterval that facilitates testing time-dependent code without modifying global state. It provides multiple aliases (e.g., timer.timeout, timer.to, timer.t) for convenience and validates arguments against NaN and non-function values. Version 0.3.0 is stable but appears unmaintained (last release 2012). Unlike sinon's fake timers, timer-shim only stubs calls without advancing time. Its key differentiator is simplicity and avoidance of global overrides, making it suitable for isolated unit tests.","status":"abandoned","version":"0.3.0","language":"javascript","source_language":"en","source_url":"git://github.com/chakrit/timer-shim","tags":["javascript","timer","setTimeout","setInterval","nextTick"],"install":[{"cmd":"npm install timer-shim","lang":"bash","label":"npm"},{"cmd":"yarn add timer-shim","lang":"bash","label":"yarn"},{"cmd":"pnpm add timer-shim","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package uses CommonJS; no ES module export.","wrong":"import timer from 'timer-shim'","symbol":"default export","correct":"const timer = require('timer-shim')"},{"note":"Both argument orders work, but the standard is (delay, fn).","wrong":"timer.timeout(fn, 100)","symbol":"timer.timeout","correct":"timer.timeout(100, fn)"},{"note":"Use timer.clear instead of global clearTimeout for consistency.","wrong":"clearTimeout(handle)","symbol":"timer.clear","correct":"timer.clear(handle)"}],"quickstart":{"code":"const timer = require('timer-shim');\nlet count = 0;\nconst handle = timer.interval(50, () => {\n  count++;\n  console.log(count);\n  if (count === 5) timer.clear(handle);\n});\ntimer.timeout(200, () => console.log('done'));\n\n// Stubbing example:\nconst sinon = require('sinon');\nsinon.stub(timer, 'timeout').callsArg(1);\nlet internalState = false;\nfunction testFn() {\n  timer.timeout(100, () => { internalState = true; });\n}\ntestFn();\nconsole.log(internalState); // true (due to stub)\ntimer.timeout.restore();","lang":"javascript","description":"Demonstrates basic usage of timer.interval and timer.timeout, then shows how to stub timer.timeout with sinon for testing."},"warnings":[{"fix":"Consider using sinon.useFakeTimers or lolex for modern timer testing.","message":"Package is unmaintained since 2012. No updates for over a decade.","severity":"deprecated","affected_versions":"all"},{"fix":"Be consistent: use (delay, function) to avoid ambiguity.","message":"Both argument orders (timeout, fn) and (fn, timeout) are supported. May confuse developers expecting strict order.","severity":"gotcha","affected_versions":"all"},{"fix":"Create a custom .d.ts file or use @ts-ignore.","message":"Package does not provide a TypeScript declaration file. No compile-time type checking.","severity":"gotcha","affected_versions":"all"},{"fix":"Use timer.clear for both, but note that it internally calls clearTimeout. Works for both since handles are just numbers.","message":"timer.clear is aliased to clearTimeout; it does not handle interval handles separately. Both timeouts and intervals are cleared with the same function.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the package is installed and use require('timer-shim'). For ES6, use const timer = require('timer-shim');","cause":"Importing the package incorrectly (e.g., using ES6 import in a CommonJS project) or the package was not installed.","error":"TypeError: timer.timeout is not a function"},{"fix":"Verify the require statement: const timer = require('timer-shim'); then call timer.clear(handle);","cause":"Attempting to call timer.clear before the module is fully loaded or using a different variable name.","error":"timer.clear is not a function"},{"fix":"Add: const timer = require('timer-shim'); at the top of the file.","cause":"The timer variable was not declared or scoped incorrectly.","error":"ReferenceError: timer is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}