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.