I'm writing a web application (HTML/CSS/JS frontend, C# backend) where the users are expected to be on the same page for a while (usually longer than 10 minutes). I would like to periodically check the server to see if the user's session has expired. Is there any disadvantage to using setInterval with an interval of say, 2 - 5 minutes, to make an AJAX request to the server?
Asked
Active
Viewed 4,275 times
2 Answers
8
No, there is no disadvantage. However, the timer isn't the most accurate. See also: Will setInterval drift? This may not matter to you, as it is a bigger problem for timers fired rapidly. Plus, it doesn't soundl ike exact timing is a problem for you either.
-
Thanks for the answer! You are correct, accuracy isn't a problem for me. I just need it to happen roughly every few minutes. – mittmemo Aug 13 '12 at 17:00
6
There is no problem in doing that. There will be a performance issue only when you don't keep the interval ids and run a number of intervals simultaneously (especially when the delay is very less). To prevent this you can clear the current interval before creating a new one. This makes sure that there is only required interval is live.
clearInterval(intervalID);
intervalID = setInterval(function(){
...
}, 100);

Diode
- 24,570
- 8
- 40
- 51
-
Thanks! That's what I figured: multiple concurrent timers present performance issues. Just wanted to be sure! – mittmemo Aug 13 '12 at 17:04