我有一个 Javasript 函数,它总是无法运行。该函数位于 jQuery.bind 下,它附加到页面上的视频播放器。一旦视频播放器准备就绪,该函数就应该运行,该函数的某些元素取决于视频的持续时间,只有在视频播放器准备就绪后才能获得该持续时间。
它似乎在大多数情况下都有效,但有时(3/10 次)却不起作用。不确定为什么会发生这种情况。
ML = (typeof ML === "undefined") ? {} : ML;
ML.player = null;
//Initialization of the video player
jQuery(document).ready(function() {
var vidId = vidIdRaw.split("/");
ML.player = new SV.Player({
videoId: vidId[0]
});
//More functions are initialized under the document.ready
//Waits for video player to be ready before executing function
//This is the function that doesn't always run
ML.player.bind('ready', function(obj) {
var isReady = true;
jQuery('#duration-time').html(formatTime(ML.player.getDuration()));
//Functions initialized above are called and run here as well.
}
}
我省略了大部分函数,因为我必须包含很多代码。我已经包含了相关信息。
最佳答案
尝试在播放器初始化后尽快绑定(bind)ready事件;提到的“更多功能已初始化...”代码可能需要很长时间才能执行,并且您错过了播放器上的就绪事件。
关于Javascript 函数并不总是在元素准备好后加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37439038/