Yes, you could use these and add them as event listeners one time via a clientside callback.
The cool thing about doing it in JS, you can tell the browser to navigate, and you dont have to worry about that extra step. If you want to display the alert via a callback, then you will have to figure out a way to trigger a callback without also resetting the timer.
Here is a js script you can put into your assets folder:
let timer, currSeconds = 0;
function resetTimer() {
/* Hide the timer text */
document.querySelector(".logout_queue")
.style.display = 'none';
/* Clear the previous interval */
clearInterval(timer);
/* Reset the seconds of the timer */
currSeconds = 0;
/* Set a new interval */
timer =
setInterval(startIdleTimer, 1000);
}
// Define the events that
// would reset the timer
window.onload = resetTimer;
window.onmousedown = resetTimer;
window.ontouchstart = resetTimer;
window.onclick = resetTimer;
window.onkeypress = resetTimer;
function startIdleTimer() {
/* Increment the
timer seconds */
currSeconds++;
if (currSeconds > 30) {
window.location.href = '../logout'
}
/* Set the timer text
to the new value */
document.querySelector(".timeout")
.textContent = 31 - currSeconds;
/* Display the timer text */
document.querySelector(".logout_queue")
.style.display = 'flex';
}
And the app layout and callback:
app.layout = html.Div(
[
html.A('logout', href='../logout'),
html.Br(),
dash.page_container,
html.Div(
[html.Div(['You will be logged out in: ', html.Span(className='timeout')],
style={'backgroundColor':'white'}, id='testing')],
className='logout_queue',
id='logout_queue',
style={'display':'none', 'height':'100vh', 'width':'100vw',
'backgroundColor':'rgba(0,0,0,0.4)', 'position':'absolute',
'justifyContent':'center', 'alignItems':'center',
'top':'0px', 'left':'0px'},
)
]
)
app.clientside_callback(
"""
function () {
resetTimer();
window.dash_clientside.no_update
}
""",
Output('logout_queue','id'), Input('logout_queue','id'))
This will display like this:
And then redirect to the logout page after 30 seconds.
In the above example, the login / logout flow is outside of the dash app, so you dont have to account for it looping and redirecting to the logout from the login page. XD
You can ignore the locations from the window.location.href when you dont want to do the logout flow.
You can bring all of this js code inside of the clientside callback if you want, instead of having it in your assets folder.