我正在使用jQuery.YoutubeBackground.js在我的网站上播放背景视频。 https://github.com/rochestb/jQuery.YoutubeBackground
jQuery(document).ready(function($) {
var promise = $('#homevideo').YTPlayer({
fitToBackground: true,
videoId: '123456', // YouTube video ID
mute: false, // Change to true to mute music
playerVars: {
modestbranding: 0,
autoplay: 1,
controls: 0,
showinfo: 0,
branding: 0,
rel: 0,
autohide: 0
},
callback: function() {
}
});
});
我正在尝试为我的代码添加承诺。
if (promise !== undefined) {
promise.then(_ => {
console.log("Autoplay started!");
}).catch(error => {
console.log("Autoplay Not started!");
});
}
我收到错误Uncaught TypeError:promise.then不是函数。我认为这可能与if (promise !== undefined)
部分的位置有关?
非常感谢!
答案 0 :(得分:0)
YTPlayer
函数不会返回承诺,因为您可以看到here。如果你在初始化之后不想运行某些东西,你可以在options对象的回调中编写该逻辑:
$('#homevideo').YTPlayer({
fitToBackground: true,
videoId: 'FlsCjmMhFmw',
mute: false,
playerVars: {
modestbranding: 0,
autoplay: 1,
controls: 0,
showinfo: 0,
branding: 0,
rel: 0,
autohide: 0
},
callback: function() {
console.log("Autoplay started!");
}
});