Приостановите воспроизведение видео YouTube с консоли Chrome через 3 минуты.

Я могу приостановить видео с globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].pause()

Я могу узнать текущее время видео с globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].currentTime

Как автоматически вызвать паузу при достижении видео currentTime > 180?

1 ответ

Решение

Простой способ - опрос.

const player = globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0]
setTimeout(function polling() {
    if (player.currentTime < 180) 
        setTimeout(polling, 1000)
    else
        player.pause()
}, 1000)

Альтернативный способ:

const player = globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0]
const timer = setInterval(() => { // no need of named function as no 'async recursion' is needed
    if (player.currentTime >= 180) {
        player.pause()
        clearInterval(timer)
    }
}, 1000)