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 undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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.