7
\$\begingroup\$

I'm trying to check in my meteor app for a defined timeout (idle user) to show a modal. But the way I did it seems a bit too complicated to me.

I'm doing a setInterval for 1 minute. Then a counter - which is stored in a Session var (maybe a local reactive var would be smarter?) - will be incremented by one. If there is any event (mousemove, keypress and so on), the counter will be reset.

Template.example.onRendered(function() {
 Session.set('idleTime', 0);
 //Increment the idle time counter every minute.
 var idleInterval = setInterval(timerIncrement, 60000);
 function timerIncrement() {
 idleTime = Session.get('idleTime');
 idleTime = idleTime + 1;
 Session.set('idleTime', Session.get('idleTime'));
 if (idleTime > 14) {
 // show modal after 15 minutes
 }
 }
});
Template.example.events({
 'mousemove, mousedown, touchstart, keypress': function() {
 Session.set('idleTime', 0);
 }
});
janos
113k15 gold badges154 silver badges396 bronze badges
asked Dec 12, 2015 at 16:47
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I think interval is overkill why not something like this:

var timer;
Template.example.onRendered(function() {
 timer = setTimeout(showModal, 60000* 15);
});
Template.example.events({
 'mousemove, mousedown, touchstart, keypress': function() {
 clearTimeout(timer);
 timer = setTimeout(showModal, 60000* 15);
 }
});
showModal = function(){};

Edit:

You just want to wait for 15 minutes so set a time out for that time and eventually reset timer. Create a timer variable that will hold the timer id so you can reset (delete&recreate) when a user do something.

answered Dec 15, 2015 at 20:12
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.