2016-12-07 50 views
1

我使用函數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

這是預期的行爲 - 您已將超時設置爲「0」,因此它會立即執行。你期望發生什麼? –

+0

@RoryMcCrossan我想要在doPlaySequence用完之後調用drawConfig(視頻播放結束),但它立即被調用。 –

+1

您能否將'doPlaySequence()'的代碼添加到您的問題中。 –

回答

1

要只運行drawConfig功能後的視頻完成您可能需要從影片ended事件處理函數中調用它,或創建一個在該事件中解決的承諾。試試這個:

function doPlaySequence() 
{ 
    var deferred = $.Deferred(); 

    // your code... 

    var opplay = document.createElement('video'); 
    // opplay logic here... 

    opplay.addEventListener("ended", function(e) { 
    // video ended logic here... 

    deferred.resolve(); 
    } 

    return deferred; 
} 

doPlaySequence().then(drawConfig); 

你會看到doPlaySequence函數現在返回遞延這是隻有解決了視頻一旦停止播放。然後,drawConfig將被調用。

+0

謝謝,讓我試試。 –

+0

是的,作品,再次感謝你! –

1

video.js播放器有一個結束事件,你應該聽,你應該解決你的發件人對象的帖子。

在你doPlaySequence()做的,

myPlayer.on('ended', function() { 
    dtd.resolve(); 
}); 

您可能需要您的DTD對象傳遞給doPlaySequence,或簡單地創建這裏面doPlaySequence,因爲它是唯一的目的是要等到視頻完全發揮。

設置超時將不符合您的目的。

+0

謝謝,讓我試試。 –

+0

是的,我知道了。我應該在整個視頻結束時,在「其他」分支上解決它。我學會了代碼的順序執行,除了視頻和圖片。 –

+0

爲了簡化代碼,我必須使用closure而不是將整個視頻代碼寫入等待函數? –