How to call a function within an addeventlistener?

Asked

Viewed 607 times

-1

I wonder if it is possible to call a function within a given event using addeventlistener().

I have this function, which adds a reward to the database :

    //Função que adiciona uma recompensa ao banco de dados
addReward(){
    if(!this.isEditing){
    this.addDocument(this.caminho, this.recompensa).then(()=>{
      //this.loadData();//refresh view
    });
  }else{
    this.updateDocument(this.caminho, this.recompensa.key, 
    this.recompensa).then(()=>{
      this.loadData();//refresh view
    });
  }
  this.isEditing = false;
  //clear form
  this.recompensa.valor;
  this.recompensa.data;
}

here I have the function that displays a particular ad to the user, I need that when the user closes the video, the above function is executed by sending the value of its reward to the database, but always returns me an error :

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((result) => {
        carregar.dismissAll();
        this.recompensa.data = moment().format('L');
        this.recompensa.valor = 0.03;
        //tento fazer assim atualmente, porém ele sempre retorna um erro quando fecho o vídeo
        document.addEventListener('admob.rewardvideo.events.CLOSE', function(event) {
            console.log(event)
            this.addReward(this.recompensa);
        })
    }), carregar.dismissAll();   
}

the error that returns me is from the image below :

inserir a descrição da imagem aqui

1 answer

1

Directly answering the question:

How to call a function within an addeventlistener?

Eventtarget.addeventlistener() needs at least two parameters. One type of event and a Listener, which can be an Eventlistener-like object or, more simply, a function as in the example below.

const element = document.querySelector('.js-button')

element.addEventListener('click', function() {
  alert('Obrigado!')
})
<div class="js-button"> CLICA EM MIM </div>

Responding to the context:

The error in your code is in calling a this.addReward that does not exist. If you give a console.log to this inside addeventlistener you will see that this method does not exist because this corresponds to the context of the System.

document.addEventListener('admob.rewardvideo.events.CLOSE', function(event) { console.log(event) this.addReward(this.recompensa); })

I don’t know how your code is formatted but maybe just call addReward(this.recompensa) resolve.

  • Thank you so much for the explanation. However if I try to put only addReward(this.reward). It returns me a syntax error, and even allows me to run the application to test. I’m working on Ionic, so I’m calling it through this, if not it returns me just this syntax error. Testing the console.log, I got a #Document on the console when printing this.

  • I have zero experience in Ionic but found a link that might help.

  • Thank you very much, I will check this link. This seems to be exactly what I need. Thanks for the help and availability.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.