This is a common code, but the lines are specifically interested in. Var timeoutCount = #{timeoutBean.getTimeout()}; and var sessionTimeOut = timeoutCount; how can you simplify here?

 var timeoutCount = #{timeoutBean.getTimeout()}; var sessionTimeOut = timeoutCount; function startTimer(timeoutCount) { if (#{request.userPrincipal != null}) { if (timeoutCount == 0) { window.location.href = '/logout?sessionTimeOut=' + sessionTimeOut; } setTimeout(function () { startTimer(timeoutCount - 1); }, '1000'); } } 

timer starts with <body onload="startTimer(timeoutCount)">

Shifted from stackoverflow.com Nov 22 '17 at 12:27 .

The question was originally posted on the site for professional and enthusiast programmers.

  • And what is this general construction #{} ? Any template engine? Ruby? Java? - Darth
  • Darth, this is from the jsf area. Sets the start time, how much the counter should count. - noxi
  • Why it is impossible to make simply var sessionTimeOut = #{timeoutBean.getTimeout()}; and everywhere instead of timeoutCount sessionTimeOut ? Where else do you use sessionTimeOut ? Maybe it is not just done? - Klimenkomud

1 answer 1

We start the counter from 0:

 const sessionTimeOut = #{timeoutBean.getTimeout()}; function startTimer(timeoutCount = 0) { if (#{request.userPrincipal != null}) { if (timeoutCount >= sessionTimeOut) { window.location.href = '/logout?sessionTimeOut=' + sessionTimeOut; } setTimeout(startTimer, 1000, timeoutCount + 1); } } 

timer starts with <body onload="startTimer()">

UP is even easier:

 if (#{request.userPrincipal != null}){ const sessionTimeOut = #{timeoutBean.getTimeout()}; window.onload = () => setTimeout(() => { window.location.href = '/logout?sessionTimeOut=' + sessionTimeOut; }, sessionTimeOut * 1000); } 

And in body onload it is not necessary to specify

  • Darth, I mean it is not clear? The problem is in these two lines, var timeoutCount = # {timeoutBean.getTimeout ()}; var sessionTimeOut = timeoutCount. In order not to make an extra request # {timeoutBean.getTimeout ()} should be simplified so that 1 request remains, but at the same time there is no reassignment. MB, can I do something about it? - noxi
  • @Alexander Plyuschev, you have one variable used as a counter, and the second stores the original value. You can run the counter from 0 to the original value, now update the answer - Darth
  • I'll check it out now) - noxi
  • @AlexanderPlyuschev And why do you have to pull the timeout every second? setTimeout( --тут logout-- , sessionTimeOut * 1000) not easier right away setTimeout( --тут logout-- , sessionTimeOut * 1000) - Darth
  • and how it will work?) Show the code how it should look like - noxi