I followed this question Login Screen For My Shiny App Does Not Time Out, but I want to add the warning alert before time out. When the alert window popped up, but then it got stuck and the app never close even though the time ran out.
Any thought?
library(shiny)
library(leaflet)
timeoutSeconds <- 60
inactivity <- sprintf("function idleTimer() {
var t = setTimeout(logout, %s);
var timeout = setTimeout(alertFunc, %s);
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer; // catches mouse clicks
window.onscroll = resetTimer; // catches scrolling
window.onkeypress = resetTimer; //catches keyboard actions
function alertFunc(){
alert('You are about time out!')
t = setTimeout(logout, %s);
}
function logout() {
Shiny.setInputValue('timeOut', '%ss')
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, %s); // time is in milliseconds (1000 is 1 second)
}
}
idleTimer();", timeoutSeconds*1000, timeoutSeconds*500,timeoutSeconds*500,timeoutSeconds, timeoutSeconds*1000)
ui <- fluidPage(
tags$script(inactivity),
leafletOutput("mymap")
)
server <- shinyServer(function(input,output,session){
observeEvent(input$timeOut, {
print(paste0("Session (", session$token, ") timed out at: ", Sys.time()))
showModal(modalDialog(
title = "Timeout",
paste("Session timeout due to", input$timeOut, "inactivity -", Sys.time()),
footer = NULL
))
session$close()
})
points <- eventReactive(input$recalc, {
cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite,options = providerTileOptions(noWrap = TRUE)) %>%
addMarkers(data = points())
})
})
runApp(list(ui = ui, server = server))