How can I prevent the function from being called again using javascript?


arrive

I'm using the youtube iframe api to detect when a video starts playing, sometimes it can happen that the state changes to buffering and then playing again, so it does this myFunctionmultiple times per session, how can I make it run only once?

if (event.data == YT.PlayerState.PLAYING) {
    playing = true;
    player.mute();
    player.setPlaybackQuality('small');

    function myFunction() {
        setTimeout(function(){
            var videoData = player.getVideoData();
            var video_id = videoData['video_id'];
            const Http = new XMLHttpRequest();
            const url='https://url?viewcounter=' + video_id;
            Http.open("GET", url);
            Http.send();
        }, 3000);
    }
}
Control AltDel

use boolean flag

var alreadyRun = false;
if (event.data == YT.PlayerState.PLAYING && !alreadyRun) {
            playing = true;
            alreadyRun = true;
            player.mute();
            player.setPlaybackQuality('small');
    function myFunction() {
      setTimeout(function(){
        var videoData = player.getVideoData();
        var video_id = videoData['video_id'];
        const Http = new XMLHttpRequest();
        const url='https://url?viewcounter=' + video_id;
        Http.open("GET", url);
        Http.send();
      }, 3000);
    }
}

Related


How can I prevent the update from being called so frequently?

quick-witted I'm using ThreeJs for a Tetris type demo. For rendering, I use requestAnimationFrame. Fragment: game.prototype.render = function(){ var thisObj = this; requestAnimationFrame( function() {thisObj.render();} ); var now = new Date().getTime();

How can I prevent the method from being called twice?

Uetani I want to programmatically check if the function was called. If it has been called before, it is not allowed to ignore it for the second time, and if it is not called, let the button be called. Why I need to do this is because I have a longClick button

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

How can I prevent my Javascript files from being cached?

Bdfy: I have a simple html: <html> <body> <head> <meta charset="utf-8"> <meta http-equiv='cache-control' content='no-cache'> <meta http-equiv='expires' content='0'> <meta http-equiv='pragma' content='no-cache'> <script src="test.js"></script> </body> </html>

How can I prevent my Javascript files from being cached?

Bdfy: I have a simple html: <html> <body> <head> <meta charset="utf-8"> <meta http-equiv='cache-control' content='no-cache'> <meta http-equiv='expires' content='0'> <meta http-equiv='pragma' content='no-cache'> <script src="test.js"></script> </body> </html>

How can I prevent my Javascript files from being cached?

Bdfy: I have a simple html: <html> <body> <head> <meta charset="utf-8"> <meta http-equiv='cache-control' content='no-cache'> <meta http-equiv='expires' content='0'> <meta http-equiv='pragma' content='no-cache'> <script src="test.js"></script> </body> </html>