0

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))
Ben
  • 181
  • 6
  • When do you want to show the warning? what happens after the warning? what do you mean you got stuck? The session is closed after the pop-up and you can no longer interact with it. The app is closed at this point, what do you mean it never closes? Please be clear in your description. – lz100 Apr 18 '22 at 22:26
  • I tried to add a pop up window for a warning purpose before time out. When I add a function ```function alertFunc(){ alert('You are about time out!') t = setTimeout(logout, %s); }```, the window showed up, then the app never closed after the second I set – Ben Apr 19 '22 at 20:03

0 Answers0