How to prevent an addEventListener from running multiple times in a function?

0

I will explain my situation. I have a function that displays a particular ad to the user in the application. After viewing this ad, I call an addEventListener, which passes a CLOSE event. After this CLOSE event is triggered, the user is rewarded for the video (as I commented in the code snippet).

What happens: When I watch a video, it adds the reward normally, just once as it should. But when I watch the second video, it adds the reward twice, so I understand that the event is running twice a second time, as if it were "saving" the previous event, and calling the new event. If I see it for the third time, it happens the same, it triggers the event 3 times, and adds the reward 3 times there in the database.

As I'm new to javascript, I need help.

How can I handle this event, so that: The user sees a video, sends the reward to the bank, kills the event. The user sees the second video, triggers the event only once, and sends the reward (a reward) to the database, and so on.

Here's the snippet of my code:

//Vídeos da Inmobi
videosInmobi() {
    let carregar = this.loading.create({content : "Carregando..."});
    carregar.present();
    let rewardConfig: AdMobFreeRewardVideoConfig = {
        isTesting: true,
        autoShow: true,
        id: 'ca-app-pub-8000726989219599/6974786599' //id videos InMobi
    };
    this.admob.rewardVideo.config(rewardConfig);
    this.admob.rewardVideo.prepare().then(() => {
        carregar.dismissAll();
    })
    //Após o usuario assistir o vídeo, chamo esse evento CLOSE, que fecha o video e executa a função
    //this.addReward(); que envia o valor da recompensa ao banco de dados.
    document.addEventListener('admob.rewardvideo.events.CLOSE', () => {
        this.recompensa.data = moment().format('L'); //aqui recebe a data da recompensa
        this.recompensa.valor = 0.02; //aqui recebe 2 centavos a cada recompensa, esse valor é enviado ao banco
        console.log('valor da recompensa : ' +this.recompensa.valor);
        console.log('data : ' +this.recompensa.data);
        this.addReward();
    });       
}

attached image to better explain what is happening:

Note: I'm working with ionic3, but this specific plugin is pure js.

    
asked by anonymous 12.11.2018 / 11:48

2 answers

0

As I recall, there is no way to check for the existence of a listener for an event. In this case, each time the addEventListener function is called, you add another anonymous function (your case) to be called in sequence. That is, each reward, you will have another event being called. You would have to call addEventListener to do this only once

  • You can try adding a Boolean value if the event was added or not (remembering that it can not be a scope variable).

  • Call document.addEventListener('admob.rewardvideo.events.CLOSE', () = at page initialization, where it would be called only once.

  • There is also the document.removeEventListener function that does exactly the opposite of 'addEventListener' except that does not work with anonymous functions, so you would have to create an external function.

  • Take a look at this link: How to check wheter dynamically attached event listener exists or not

    PS: Sorry for not giving sample code, I'm not from the javascript area but I tried to help with some information.

        
    12.11.2018 / 12:09
    0

    Thanks guys for the answers, I was able to resolve the issue by calling addEventListener on page loading as suggested.

    Then the code looks like this:

        ionViewWillEnter() {
        this.showBanner();
        document.addEventListener('admob.rewardvideo.events.CLOSE', () => {
            this.recompensa.data = moment().format('L'); //aqui recebe a data da recompensa
            this.recompensa.valor = 0.02; //aqui recebe 2 centavos a cada recompensa, esse valor é enviado ao banco
            console.log('valor da recompensa : ' +this.recompensa.valor);
            console.log('data : ' +this.recompensa.data);
            this.addReward();
        });  
    }
    
        
    12.11.2018 / 12:50