FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

useIdle

25mins

Implement a function useIdle(timeout) that detects user inactivity.

The function should:

  • Accept a timeout in milliseconds.
  • Return a function that gives true if the user has been idle for the given duration, otherwise false.
  • Consider mouse movements, key presses, and touch events as user activity.
  • Reset the idle timer whenever the user interacts.

Example

const isIdle = useIdle(5000); // returns a function // Call the returned function to check if idle console.log(isIdle()); // false initially // After 5 seconds of no activity console.log(isIdle()); // true

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests