我正在使用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!");
});
}
我收到错误未捕获的类型错误:promise.then不是一个函数。我相信这可能与if (promise !== undefined)
部分的位置有关?
非常感谢帮助!
YTPlayer
函数不返回 promise ,如下所示。如果你不想在初始化后运行某些东西,你可以在选项对象的回调中编写该逻辑:
$('#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!");
}
});