从控制台日志中删除不必要的错

时间:2015-07-24 06:46:51

标签: jquery html css youtube-api

我有这个自定义YouTube嵌入了一些整洁的小工具,它运作完美,但有一个问题。控制台中有许多错误,例如:

Uncaught TypeError: Cannot read property 'getVolume' of undefined
index.html:189 Uncaught TypeError: player.getVolume is not a function
index.html:179 Uncaught TypeError: player.getCurrentTime is not a function
index.html:183 Uncaught TypeError: player.getCurrentTime is not a function

我不知道可能导致这些错误的原因,我已尝试使用代码执行许多操作,例如移动和重新分配变量,我似乎无法清除它们,有什么帮助吗?

CODEPEN: http://codepen.io/mistkaes/pen/MwONzK

1 个答案:

答案 0 :(得分:2)

尝试使用播放器中的onReady方法向其中触发间隔,因此您将确保已定义player变量。

function onYouTubeIframeAPIReady() {
    var rangerGo;
    var player = new YT.Player('player', {
        height: '282',
        width: '502',
        videoId: 'QExOaGT_ids',
        playerVars: {
            'controls': 0,
            'showinfo': 0,
            'iv_load_policy': 3,
            'rel': 0,
        },
        events: {
            onReady: function(){
                setInterval(function() {
                    $("#content").text("video_time: " + player.getCurrentTime().toString().toHHMMSS());
                }, 250);

                rangerGo = setInterval(function() {
                    $("#range").slider("value", player.getCurrentTime());
                    $("#range").slider("option", "max", player.getDuration());
                }, 250);

                setInterval(function() {
                    // VOLUME CONTROLS
                    $("#volume-amount").text("volume: " + player.getVolume() + "%");
                    player.setVolume($("#volume-range").slider("value"));
                }, 1);

                $("#range").slider({
                    range: "min",
                    start: function(event, ui) {
                        player.pauseVideo();
                        clearInterval(rangerGo);
                    },
                    stop: function(event, ui) {
                        player.seekTo(ui.value, true);
                        player.playVideo();
                        rangerGo = setInterval(function() {
                            $("#range").slider("value", player.getCurrentTime());
                            $("#range").slider("option", "max", player.getDuration());
                        }, 250);
                    }
                });
            }
        }
    });
}