我使用函數doPlaySequence
播放多個視頻,然後用功能drawConfig
顯示圖片。我寫了這樣的代碼:jQuery承諾:一次調用回調函數
function wait(dtd) {
var dtd = $.Deferred();
setTimeout(function() {
doPlaySequence();
dtd.resolve();
}, 0);
return dtd.promise();
}
wait().then(drawConfig);
然而drawConfig
一次被稱爲同時影片還沒有結束。我只是複製了這個例子,所以代碼可能看起來很醜。我使用video.js來幫助播放視頻。
var stropsrc = new Array(3);
stropsrc[0]= "../rs/MOVIE/KlogoPk.mp4";
stropsrc[1]="../rs/MOVIE/theme.mp4";
stropsrc[2]= "../rs/MOVIE/OpPk.mp4";
function doPlaySequence()
{
var divcontainer = document.createElement("div");
divcontainer.setAttribute("id","divop");
document.body.appendChild(divcontainer);
var playID = "op0";
var opplay = document.createElement('video');
opplay.setAttribute("id",playID);
opplay.preload = "none";
divcontainer.appendChild(opplay);
opplay.src = stropsrc[0];
var AttrObj = {"datacount":0};
var myPlayer;
videojs.setAttributes(opplay,AttrObj);
myPlayer = videojs(opplay);
myPlayer.play();
opplay.addEventListener("ended",function(e)
{
e.preventDefault();
var c = parseInt(this.getAttribute("datacount"))+1;
if(c < 3)
{
opplay.src = stropsrc[c];
AttrObj.datacount = c;
videojs.setAttributes(opplay,AttrObj);
myPlayer = videojs(opplay);
myPlayer.play();
}
else//end
{
/*reset*/
opplay.src = stropsrc[0];
AttrObj.datacount = 0;
videojs.setAttributes(opplay,AttrObj);
/*hide*/
var divContainer = document.getElementById("divop");
divContainer.style.display = "none";
divContainer.style.visibility = "hidden";
var divOP = document.getElementById("op0");
divOP.style.display = "none";
divOP.style.visibility = "hidden";
}
});
這是預期的行爲 - 您已將超時設置爲「0」,因此它會立即執行。你期望發生什麼? –
@RoryMcCrossan我想要在doPlaySequence用完之後調用drawConfig(視頻播放結束),但它立即被調用。 –
您能否將'doPlaySequence()'的代碼添加到您的問題中。 –