当 YouTube API iFrame 覆盖视频的 75% 以上时执行一次函数
Executing a function once when YouTube API iFrame hits over 75% of the video
当 iFrame 通过 YouTube API 播放时,我试图 运行 一个函数,但它执行了太多次。我试图用 bool 标记该函数无济于事。我已经坚持了几个小时。请帮忙,哈哈。
如何在视频播放时将此功能限制为运行一次?
var completedMajority = false;
function onPlayerStateChange(event, tID, tName, isThereAnExam, time) {
if (event.data == 1) { // playing
myTimer = setInterval( function(){
var time;
time = player.getCurrentTime();
minutes = parseInt(time / 60, 10);
seconds = parseInt(time % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
timeDuration = localStorage['timeDuration'];
majorityOfVid = timeDuration / 4 * 3;
console.log(time);
if (!completedMajority && time >= majorityOfVid) {
completedMajority = true;
majorityOfModuleCompleted(isLive, baseURL)
}
console.log(completedMajority);
$('#thisIsTheFinalCountdown').text(minutes + ":" + seconds);
}, 100);
}
else { // not playing
clearInterval(myTimer);
}
在您的第一个 if
之前的整个函数应该包含在:
if (!completedMajority) {
[...]
}
当 iFrame 通过 YouTube API 播放时,我试图 运行 一个函数,但它执行了太多次。我试图用 bool 标记该函数无济于事。我已经坚持了几个小时。请帮忙,哈哈。
如何在视频播放时将此功能限制为运行一次?
var completedMajority = false;
function onPlayerStateChange(event, tID, tName, isThereAnExam, time) {
if (event.data == 1) { // playing
myTimer = setInterval( function(){
var time;
time = player.getCurrentTime();
minutes = parseInt(time / 60, 10);
seconds = parseInt(time % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
timeDuration = localStorage['timeDuration'];
majorityOfVid = timeDuration / 4 * 3;
console.log(time);
if (!completedMajority && time >= majorityOfVid) {
completedMajority = true;
majorityOfModuleCompleted(isLive, baseURL)
}
console.log(completedMajority);
$('#thisIsTheFinalCountdown').text(minutes + ":" + seconds);
}, 100);
}
else { // not playing
clearInterval(myTimer);
}
在您的第一个 if
之前的整个函数应该包含在:
if (!completedMajority) {
[...]
}