Ich habe eine interval
innerhalb onPlayerStateChange
Funktion, die den progress
Elementwert unter Verwendung requestAnimationFrame
aktualisiert. Das Problem ist, dass die CPU-Auslastung von 90% to 170%
geht. Ich habe versucht, den requestAnimationFrame zu löschen, wenn der Fortschrittswert derselbe wie Y param ist und wenn das Video nicht spielt, aber es immer noch das selbe ist. Auch console.log('Y')
protokolliert tausende Male. Es protokolliert auch wenn ich das Video pausiere, es wird einfach nicht aufhören.requestAnimationFrame CPU 170%
Was ist das Problem hier?
var pFrame;//global
function scrollP(Y, duration, easingFunction) {
var start = Date.now(),
elem = document.getElementById('progress'),
from = elem.value;
if(from === Y) {
window.cancelAnimationFrame(pFrame);
return; /* Prevent scrolling to the Y point if already there */
}
function min(a,b) {return a < b ? a : b}
function scroll() {//this is the animatin frame
console.log('Y');
var time = min(1, ((Date.now() - start)/duration)),
easedT = easingFunction(time);
elem.value = (easedT * (Y - from)) + from;
pFrame = requestAnimationFrame(scroll);
}
pFrame = requestAnimationFrame(scroll)
}
function onPlayerStateChange(event) {
//clear interval when not playing
//clear pFrame
if(player.getPlayerState() != 1) {
window.cancelAnimationFrame(pFrame);
clearInterval(to);
}
if(event.data == YT.PlayerState.PLAYING) {
to = setInterval(function() {
(duration > 0) ? value = 1/duration * player.getCurrentTime() : 0;
//here I call the pFrame function
pFrame = scrollP(value, 1000, easing.linear)
}, 50);
}
}
Sie setzen 'pFrame' bei jedem Start einer Schleife auf' undefined' ('pFrame = scrollP (value, 1000, easing.linear)'), damit Sie keinen Ihrer Animationsframe-Handler löschen. –
Ist es nicht eine globale Var? der pFrame? –
Warum ist 'scrollP (value, 1000, easing.linear)' nicht definiert? –