Demo
Features | No dependencies — TypeScript — SSR support — Readable store for idle value — onIdle
callback
Installation
npm i svelte-idle
Usage
<script>
import { start, idle, onIdle } from 'svelte-idle'
// Run start on component Initialization
start()
// Run code when user idle via callback...
onIdle(() => {
console.log('User is idle')
})
//... or by using the idle store
$: {
if($idle) console.log('User is idle')
}
</script>
User is idle: {$idle}
API
start
The start method accepts an optional object (type: StartOpts
). The following values can be defined:
milliseconds
- type:
number
- defines: how long before the user is udle
throttle_milliseconds
- type:
number
- defines: amount of milliseconds before each idle-check
Example:
import { start } from 'svelte-idle'
start({
milliseconds: 60_000,
throttle_milliseconds: 500
})
idle
A readable store that reflects the current idle-state.
onIdle
Callback which will be fired everytime idle becomes true. Returns a method for clearing the listener.
Example:
import { onMoumt } from 'svelte'
import { onIdle } from 'svelte-idle'
onMount(() => {
const unsub = onIdle(() => console.log('User is idle!'))
return unsub
})