It seems that i cant access the constructor properties from an setInterval and i need to get and to modify the properties . any help on how can i solve this ?
startit() {
console.log(this.page,this.nrpages) //works
setInterval(function() {
console.log(this.currentPage,this.nrpages) // undefined undefined
Use arrow function
startit() {
console.log(this.page,this.nrpages) //works
setInterval(() => {
console.log(this.currentPage,this.nrpages);
});
The key point here is that this points to the current execution context. So inside setInterval function this is not referring to your outer execution context. You can do something like:
startit() {
console.log(this.page,this.nrpages) //works
var thiObj = this;
setInterval(function() {
console.log(thisObj.currentPage,thisObj.nrpages) //works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With