I\'m trying to create an image slider inside my Angular 5 component. The slider has Previous and Next controls which both work fine but I can\'t get it to slide automatically.
change
setTimeout(this.autoslide, 2000)
to
setTimeout(this.autoslide.bind(this), 2000)
for this
to keep pointing to the current class instance.
The problem is that when you call setTimeout
you pass the function without binding it to the current this
. In Javascript this
is determined by the caller for functions. You can capture this
from the declaration context by using arrow functions or bind
:
setTimeout(this.autoslide.bind(this), 2000)
// OR
setTimeout(()=> this.autoslide(), 2000)