0
I will explain my situation. I have a function that displays a certain ad to the user in the application. After viewing this ad, I call an addeventlistener, who 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 be. But when I watch the second video, it adds the reward twice, so I understand that the event is being run twice the second time, it’s like he’s "guarding" the previous event, and calling the new event. If I see it a third time, it happens the same, it triggers the event 3 times, and adds the reward 3 times there in the database.
Since I’m new to javascript, I need help.
How can I make the deal of this event, so : 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.
Follow the excerpt 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 occurring :
Note: I am working with ionic3, but this specific plugin is pure js.
Instead of adding the function directly to
addEventListener
, use a variable to represent the function and use theremoveEventListener
to remove the function and add another one. Or (even simpler), use the functionadmob.rewardvideo.events.CLOSE
out of functionvideosInmobi
– Valdeir Psr
I appreciate the cooperation and willingness to help. A hug and thanks again.
– Diego Estacho