2
function Scroller(id){
    this.ID = id;
    this.obj = $("#"+id);
    this.currentSlide = 1;

    var self = this;
    setInterval(self.nextSlide, 1000);
}

Scroller.prototype.nextSlide = function(){
    this.slideTo(this.currentSlide+1);
}

Scroller.prototype.slideTo = function(slide){
    // slide = (slide+this.obj.children().size()-1) % this.obj.children().size()+1; 
    // $("#"+this.ID+" > *:first-child").stop().animate({"margin-left": "-"+this.obj.get(0).offsetWidth*(slide-1)}, 1000);
    // currentSlide = slide;
}

$(document).ready(function(){
    new Scroller("actielist");
});

So this is my code for my Scroller, but when I try to run it, it gives me the following error: "Uncaught TypeError: this.slideTo is not a function"

1 Answer 1

4

When setInterval calls a function, it calls it with the context (or this value) of window (ie. it calls the function in the global scope/context). You need to make sure the context inside nextSlide is correct.

Try:

setInterval(self.nextSlide.bind(self), 1000);
Sign up to request clarification or add additional context in comments.

1 Comment

You will also likely want to save the returned value of the setInterval call so that you can clear the interval at a later point

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.