在Knockout JS中使用Youtube IFrame api时出现类型错误。视频正在ftl上加载,但是由于Player对象完全初始化之前的异步调用,所以player.getDuration()导致错误。 有没有人知道如何在Knockout JS中解决此问题?
我尝试了回调方法,然后尝试了完成方法,但是在Knockout JS中没有运气。
错误详细信息:
Uncaught TypeError: player.getDuration is not a function
at Object.getTotalDuration (youtube-player-module.require.js:28)
at list-view-module.require.js:102
at Object.onYouTubeIframeAPIReady (youtube-player-module.require.js:24)
at ViewModel.self.submitform(list-view-module.require.js:101)
at Modal.eval (eval at parseBindingsString (knockout-latest.js:68), <anonymous>:3:96)
at HTMLInputElement.<anonymous> (knockout-latest.js:89)
at HTMLInputElement.dispatch (jquery.js:5183)
at HTMLInputElement.elemData.handle (jquery.js:4991)
list-view-module.require.js
youtubeplayermodule.onYouTubeIframeAPIReady(vid,function ()
{
var duration= youtubeplayermodule.getTotalDuration();
});
youtube-player-module.require.js
let player = ko.observable();
function onYouTubeIframeAPIReady(vid, doneCallBack) {
player = new YT.Player('video-placeholder', {
videoId: vid,
events: {
'onReady': initialize
}
});
doneCallBack();
}
function getTotalDuration() {
return player.getDuration();
}
function initialize(){
alert('initiaze');
}