YouTube iframe api - why is onError event being called twice?

I’m having some trouble with the onError event in the YouTube api. Below is a script with a fake videoId to create an error which calls the function onPlayerError. Unfortunately I can’t understand why the error function gets called twice. I think it may be something to do with the callback response being inside a function (maybe?) but neither the onReady or onStateChange get called twice when the videoId is correct. Can anyone see any obvious issues? Thanks in advance.

BTW: yes I do need the callback and the response inside the callback. This is a shortened version of my complete script.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" lang="en">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- jQuery -->
<script type="text/javascript" src=""></script>

<!-- jQuery / javascript for page -->
<script type='text/javascript'>

//create deferred object
var YTdeferred = $.Deferred();

//attach a function to onYouTubeIframeAPIReady
window.onYouTubeIframeAPIReady = function() {
    //resolve when youtube callback is called passing YT as a parameter

//embed youtube api script
var tag = document.createElement('script');
tag.src = "";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

//create player variable
var player;

//create YouTube player
function createyoutubeplayer(nextPageToken)
    //whenever youtube callback was called = deferred resolved
    //your custom function will be executed with YT as an argument
    YTdeferred.done(function(YT) {
    console.log("create player");
        //create new player
        player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'fff',
            playerVars: {
                'rel': 0,
                'autohide': 1,
                'controls': 1,
                'showinfo': 0,
                'iv_load_policy': 3,
                'fs': 0,
                'modestbranding': 0,
                'playsinline': 1,
                'enablejsapi': 1
            events: {
                'onError': onPlayerError,
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange

//when YouTube player is ready
function onPlayerReady(event)
    //play video;

    //unmute sound and set the volume;;

//when YouTube video ends
function onPlayerStateChange(event)
    //if finished playing     
    if( === 0)
        //call next video function       

function onPlayerError(event)
    //call next video function  
    console.log('Error: ' +;





    <!-- video player -->
    <div id="player"></div>


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.