0
錯誤再現步驟:使用一個回調函數在Javascript
- 打開http://jaminweb.com/YoutubePlaylist.html
- 輸入
sMqNFAU0tOw
(或任何其他YouTube視頻ID)到播放列表 - 點擊播放影片!
- JavaScript控制檯應該說,像未捕獲
TypeError: undefined is not a function
在嘗試診斷問題:
的播放影片!按鈕調用下面的函數
this.playAll = function()
{
var vidtexts = document.getElementsByClassName('vidtxt');
for (var i = 0, j = vidtexts.length; i < j; ++i)
{
var thisid = vidtexts[i].value;
if (thisid.length > 0)
{
var thisurl = "http://www.youtube.com/v/" + thisid + "?enablejsapi=1&playerapiid=ytplayer&version=3";
var atts = { id: "myytplayer" };
var params = { allowScriptAccess: "always" };
swfobject.embedSWF(thisurl, "playerdiv", "425", "356", "8", null, null, params, atts, this.playVid);
}
}
}
依次調用下面的函數
this.playVid = function(e)
{
/* Callback function for swfobject.embedSWF(...), to play the video
if the flash player was loaded successfully. */
if (e.success)
{
var ytplayer = document.getElementById("myytplayer");
ytplayer.playVideo();
while (ytplayer.getPlayerState() == 1) {;} // need to change this sometime lol
}
}
控制檯錯誤指向行
ytplayer.playVideo();
所以問題是對象ytplayer
在調用playVideo()
時尚未實例化。我不明白這是爲什麼,因爲當我用我包括作爲可選參數的函數
swfobject.embedSWF(thisurl, "playerdiv", "425", "356", "8", null, null, params, atts, this.playVid);
回調函數this.playVid
我寫信給調用只有當與ID myytplayer
一個swf
對象被創建ytplayer.playVideo();
。
相關文章: