T
T
Tw1ce2016-07-12 20:07:23
go
Tw1ce, 2016-07-12 20:07:23

How to perform an action after a period of time that can change?

There is a general cycle that processes all user requests, and there is the concept of a session, after which an action should be performed, suppose (very close in meaning) a notification should be sent to the user. The session is closed after a certain time has elapsed after the last request from the user, that is, this is a delayed execution, but the time must be updated, since the countdown must go from the last request. I have several options, for example, the deferred execution function simply ends if, by the time of the last user request, it understands that it has been updated and somewhere else there is another function that will do what is required, but a little later, but, as it seems to me, this far from being the most elegant solution, and there are better ways to achieve the desired behavior. Actually, suggestions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2016-07-13
@Tw1ce

You can use the time function. AfterFunc to run a function after a certain amount of time. It returns a timer. If another request occurs, then stop the current timer using Stop and create the same new one.

E
Evgeniy Ivakha, 2016-07-14
@ivahaev

In addition to Alexey's solution, I will offer another option that I personally use to remove rotten sessions
: high accuracy is not required, then once a minute I just go through all the sessions, look at the time of the last access, and if more than the set time has passed, then I kill the session. Accordingly, each time a session is requested, the LastRequest field is set to the current time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question