02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

clearInterval

In our last video we set the setInterval and had our interval run in this video we examine how to terminate our interval and understand the way to target a specific interval.

Video Transcript

00:00Let's force it to end and the way we're going to force it to end is first of all
00:03We have to give our set interval we have to capture that integer. That's coming back, so I'm going to create another private variable
00:11I'm going to call it interval
00:14It's going to be a type of integer
00:16Although we know it's a you ends right because that's what the description told us and there's going to copy that variable and just assign
00:23The value that's coming back from our set interval into our interval variable
00:28Fantastic and now we could use our clear interval and basically call our clear
00:33interval
00:34When our seconds is completed sending into it that ID that number that it's expecting
00:40So if we run our application one more time we should be getting 2 1 0 and that's it
00:47So already we're in great a great spot only problem that we have right now is what would happen if a user calls the start timer
00:55While our application is running, right?
00:59Before it cleared an interval to solve that problem
01:02What we could do is basically every time our start timer is called just before we start a new interval to send a clear interval
01:10Now the reason this was what would work even though the first time it's called
01:14There is nothing to clear our clear forgives us for that if we send a clear interval and there the number that's we're sending
01:21Is zero, but it can't find it. It's just going to ignore it
01:24It's not going to create any errors. So as we run our application each time that we call our start time
01:30It would then first clear our interval making sure that we're not running two intervals at the same time
01:35So if I didn't go in my application
01:37I go to my two and I set a start timer and I decided to start it by ten
01:43Right after I created it even though I started my three and I run my application again
01:49our application will start from ten nine eight and the reason why we're not seeing the ten is because
01:55We're not we're waiting. First of all, we're not tracing out until we actually subtract one, right?
02:01If we would do our trace before then we would see it

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!