您可以嘗試這樣的事:
function initPlayer(playerQueue){
if(playerQueue.length > 0) {
var player = playerQueue.pop(); // get our options hash for this iteration
var container = $('<div id="'+player.container+'"></div>').appendTo('body');
$(container).jPlayer({
ready: function(){
var _e = this.element;
_e.jPlayer('setFile', player.file);
var timer = setInterval(function(){
if(_e.jPlayer('getData', 'diag.loadPercent') == 100){
$(player.control).click(function(){
// assign your handler
});
clearInterval(timer); // clear the interval
initPlayer(playerQueue); // invoke the next player init
}
},500);
},
/* ... other options ... */
});
}
}
initPlayer([
{container: 'audio-1', file: 'uri/for/file', control: '#button-1'},
{container: 'audio-2', file: 'uri/for/file', control: '#button-2'},
{container: 'audio-3', file: 'uri/for/file', control: '#button-3'},
{container: 'audio-4', file: 'uri/for/file', control: '#button-4'}
]);
我們基本上擺脫了明確的循環,而是使用現成功能本身通過initPlayer
功能,推進構建迭代。通過使用一個數組,我們可以使用pop()
,它將得到數組的最後一個元素,同時將它從數組中移除。我們等待調用initPlayer
,直到通過每500毫秒輪詢播放器的負載百分比來獲得100的負載百分比。
這只是一個建議,可能需要很多工作......我從來沒有使用過jPlayer,而且我正在從文檔中盲目飛行,所以不要期望它能夠開箱即用:-)。
你是絕對的天才!非常感謝,我真的很感激它,我不得不亂它一下,添加jPlayer('load');在我們指定文件路徑以實際加載它之後..並且jPlayer喜歡返回99.9999999而不是100,這很奇怪。我得到一個「Uncaught TypeError:不能每次讀取未定義的屬性'容器'(代碼片段中的第4行)。儘管希望儘可能達到底線......再次感謝! (: – greenimpala 2010-09-25 20:34:55
hmm throw in'console.log(player); console.log(playerQueue);'在創建了'div'的行之前,在Firefox/Firebug中進行調試,並查看每個內容的錯誤。可能會解決這個問題,但是可以通過明確地將你的對象數組附加到'window'對象並將其明確地稱爲:-) – prodigitalson 2010-09-25 21:00:05
Ahh剛剛做了你說什麼,出於某種原因我刪除了if(playerQueue.length> 0)..所以它試圖處理空數組。感謝那 :) !! – greenimpala 2010-09-25 21:44:21