Link to home
Avatar of pingeyeg

asked on

Pause and continue of setInterval

I'm trying to figure out how to get setInterval to behave the way I want.  Basically, I have the following code:

var setinterval_video = setInterval(function() {
	do something
}, 60000);

var ispaused = false;
if (!ispaused) {

Open in new window

Any help is greatly appreciated.

My objective is to be able to pause the interval and continue it somehow.  In other words, if I pause video, the setinterval_video should pause as well, but once I unpause the video, the interval should continue where it left off.  I know that clearInterval literally clears it and then you must start a new one, but there has to be a way to continue it where it left off.
Avatar of leakim971
Flag of Guadeloupe image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of pingeyeg


I'm slightly confused on this.  Can you explain how this pauses the timer when the user pauses the video and then resumes?
Also, does a clearInterval not need to be used here?
I guess what I'm confused on, which documentation is scarce on this is, if the setInterval is already enabled and I want it to pause, how does setting ispaused to true going to stop it and retain the value it had reached so that I can then continue where it left off?
unlike setInterval, setTimeout run one time. So to emulate setInterval, you put setTimeout inside a function.
This function is called by the timeout itself so you're in an endless loop UNTIL a process set to true your pause.
To restart you need to call the function.

There's NO pauseInterval or pauseTimeout
I'm completely aware of how the two methods work, but I'm more interested in how to code for pausing and continuing.  I asked around and found out a possible solution to this problem.  When I clear the timer, I should set aside the amount of time that has passed, then when the video is unpaused, I should subtract the amount of time that has passed by the 60000 time to make sure the time is keeping up.  Does that make sense?
why do you need to know how much time has passed if you want a pause.
what is the purpose of a pause ? stop the time no?
I was actually able to figure this one out after a lot of research and talking to other fellow developers.  Basically, I need to know how much time had elapsed before the pause or buffer had occurred so that I would know how long the user have viewed up to that point.  Anyway, I will award the points to you leakim971.  Thanks for your help on this.