Somethin is wrong, http request isn't called after 30 secs.
Full code
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var sw_myFunction = false;
var player, playing = false;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '100%',
width: '100%',
videoId: '<?php if (empty($vid)) {
echo "YbJOTdZBX1g";
}
else {
echo $vid;
}
?>',
playerVars: {'controls': 0, 'fs': 0,'playsinline': 1 },
events: {
'onError': onPlayerError,
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
playing = true;
player.mute();
player.setPlaybackQuality('small');
var videoData = player.getVideoData();
var video_id = videoData['video_id'];
function myFunction() {
if(sw_myFunction) return;
setTimeout(function(){
const Http = new XMLHttpRequest();
const url='https://www.url.com?viewcounter=' + video_id;
Http.open("GET", url);
Http.send();
}, 30000);
sw_myFunction = true;
}
}
else if(event.data == YT.PlayerState.PAUSED){
}
else if(event.data == YT.PlayerState.ENDED){
}
}
function onPlayerReady(event) {
}
function onPlayerError(event)
{
}
</script>
</body>
</html>
myFunctionis invoked, in addition to the definition.