Skip to content

Commit 4b90ce7

Browse files
add Idle Timer API to detect user inactivity
1 parent 6932c52 commit 4b90ce7

File tree

5 files changed

+111
-9
lines changed

5 files changed

+111
-9
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,70 @@ preventTabClose(false);
527527

528528
---
529529

530+
# ⏱️ Idle Timer API – webdev-power-kit
531+
532+
This module helps you detect when a user becomes inactive (idle) for a certain amount of time — perfect for auto-logout, showing alerts, or stopping background activity.
533+
534+
---
535+
536+
## ✨ Features
537+
538+
* Track if user is inactive for X seconds
539+
* Automatically resets on any activity (scroll, key, mouse, touch)
540+
* Lightweight and easy to use
541+
* Uses native browser events
542+
543+
---
544+
545+
## ✅ Functions
546+
547+
### 1. `startIdleTimer(seconds, onIdle)`
548+
549+
📌 Starts tracking user activity and runs your callback when idle.
550+
551+
```js
552+
import { startIdleTimer } from "webdev-power-kit";
553+
554+
// Run after 10 seconds of inactivity
555+
startIdleTimer(10, () => {
556+
console.log("💤 User is idle!");
557+
});
558+
```
559+
560+
#### 📥 Parameters:
561+
562+
| Param | Type | Description |
563+
| --------- | ---------- | ----------------------------------------------- |
564+
| `seconds` | `number` | Time in seconds to wait before calling `onIdle` |
565+
| `onIdle` | `function` | Callback function to run when user becomes idle |
566+
567+
#### 🔁 Returns:
568+
569+
* `() => void` — A function to stop and clean up the idle timer
570+
571+
---
572+
573+
## 🔐 Browser Support
574+
575+
| Browser | Supported? | Notes |
576+
| ------- | ---------- | --------------- |
577+
| Chrome || Fully supported |
578+
| Firefox || Fully supported |
579+
| Edge || Fully supported |
580+
| Safari || Fully supported |
581+
| Mobile || Fully supported |
582+
583+
---
584+
585+
## 💡 Use Cases
586+
587+
* Auto-logout after inactivity
588+
* Show “You’re idle” banner
589+
* Pause API polling when user is inactive
590+
* Lock app after idle timeout in sensitive dashboards
591+
---
592+
593+
530594

531595

532596
## 📄 License

src/idleTimer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let timeoutId = null;
2+
const activityEvents = ["mousemove", "keydown", "scroll", "touchstart"];
3+
4+
/**
5+
* Detects user inactivity and runs a callback after given seconds.
6+
* @param {number} seconds - Idle time in seconds before triggering
7+
* @param {() => void} onIdle - Callback to run when user becomes idle
8+
* @returns {() => void} - Function to stop the idle timer
9+
*/
10+
export function startIdleTimer(seconds, onIdle) {
11+
const ms = seconds * 1000;
12+
13+
const resetTimer = () => {
14+
if (timeoutId) clearTimeout(timeoutId);
15+
timeoutId = setTimeout(onIdle, ms);
16+
};
17+
18+
activityEvents.forEach((event) =>
19+
window.addEventListener(event, resetTimer)
20+
);
21+
22+
resetTimer(); // Start the initial timer
23+
24+
return () => {
25+
clearTimeout(timeoutId);
26+
activityEvents.forEach((event) =>
27+
window.removeEventListener(event, resetTimer)
28+
);
29+
};
30+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './vibration.js';
55
export * from './network.js';
66
export * from './tabVisibility.js';
77
export * from './preventClose.js';
8+
export * from './idleTimer.js';

test/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ <h2>Clipboard Test</h2>
1919
<hr>
2020
<p id="tab-status">Tab visibility: ...</p>
2121
<hr>
22+
<p id="idle">Status: Active</p>
23+
<hr>
2224
<button id="enable">Enable Tab Protection</button>
2325
<button id="disable">Disable Tab Protection</button>
2426

test/testScript.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import { sendNotification } from '../src/index.js';
33
import { getBatteryStatus } from '../src/index.js';
44
import { isOnline, listenNetworkStatus } from "../src/index.js";
55
import { listenTabVisibility } from "../src/index.js";
6-
import { preventTabClose } from "../src/index.js";
6+
import { preventTabClose } from "../src/index.js";
7+
import { startIdleTimer } from "../src/index.js";
78

8-
document.getElementById("enable").onclick = () => {
9-
preventTabClose(true);
10-
alert("Tab close protection enabled.");
11-
};
9+
startIdleTimer(5, () => {
10+
document.getElementById("idle").textContent = "💤 You are idle!";
11+
});
12+
13+
document.getElementById("enable").onclick = () => {
14+
preventTabClose(true);
15+
alert("Tab close protection enabled.");
16+
};
1217

13-
document.getElementById("disable").onclick = () => {
14-
preventTabClose(false);
15-
alert("Tab close protection disabled.");
16-
};
18+
document.getElementById("disable").onclick = () => {
19+
preventTabClose(false);
20+
alert("Tab close protection disabled.");
21+
};
1722

1823
listenTabVisibility((visible) => {
1924
document.getElementById("tab-status").textContent =

0 commit comments

Comments
 (0)