How to pause YouTube video at specific timestamp automatically

I’m working with the YouTube API and I need to automatically pause a video when it reaches a specific time. I can see there’s an option to make videos start at a certain timestamp, but I can’t find any built-in method to make them stop at a particular moment. Has anyone figured out a way to do this? Maybe there’s some kind of event listener or timer solution I’m missing. I’ve been looking through the documentation but haven’t found anything obvious. Any suggestions would be really helpful since this is a key feature I need for my project.

I’ve hit this same issue. Best solution I found: use YouTube API’s onStateChange event with requestAnimationFrame instead of setInterval or setTimeout. Set up a recursive function that calls itself with requestAnimationFrame while the video plays, checks getCurrentTime() against your target, then pauses when it hits the mark. Way smoother and more accurate than polling every 100ms. Performance is great too - no continuous intervals eating resources, and it stops checking automatically when the video pauses or buffers.

You can also use the YouTube Player API’s onStateChange event with a time check during playback. Skip the continuous polling - just attach a listener that fires during playback and check if getCurrentTime() hits your target timestamp. When it does, call pauseVideo() and remove the listener. Way more efficient since it only checks during actual playback state changes. The timing won’t be as precise as tight polling, but it’s easier on system resources and works great unless you need frame-perfect accuracy.

you could also use the YouTube API’s onStateChange callback with setTimeout instead of setInterval. when the video starts, calculate how much time’s left until your target timestamp and set one timeout. something like setTimeout(() => player.pauseVideo(), (targetTime - currentTime) * 1000). uses way less resources than constantly polling and it’s more accurate than waiting for state changes. just remember to clear the timeout if the user pauses or stops manually.

The YouTube Player API doesn’t have a built-in stop parameter, but you can work around this with getCurrentTime() and setInterval. I set up a timer that checks the playback position every 100-200ms and calls pauseVideo() when it hits your target time. Something like setInterval(() => { if (player.getCurrentTime() >= targetTime) { player.pauseVideo(); clearInterval(timer); } }, 100). Just remember to clear the interval after pausing so it doesn’t keep running. Works reliably across browsers and timing’s pretty accurate. Only thing to watch is don’t set the interval too low or you’ll hurt performance.

I had good luck combining the YouTube API with the onProgress event in a custom player wrapper. I stored the target pause time in a variable and used the player’s progress tracking to watch playback. The trick is hooking into the video’s natural progress updates instead of polling or using timeouts. You get solid accuracy without constantly checking or dealing with imprecise timeout calculations. Timing usually hits within 200-300ms of your target, which worked fine for what I needed. Just handle edge cases like seeking or speed changes that might mess with your calculations.