1

I'm trying to figure out how this custom jQuery functions work by experimenting a bit. I can't however get it to work properly. I'm trying to call the prepareSlide function in my setInterval but it says prepareSlide is not defined

$('document').ready(function(){
    jQuery('.item_holder').itemSlider({
        start: 1,
        carousel: true
    });
});
$.fn.itemSlider = function (details) {
    var currentSlideNumber = (details && details.start > 0) ? details.start : 1;
    this.prepareSlide = function(slideNumber) {
        alert(1)
    }
    //Set an interval
    var itemToSlide = currentSlideNumber + 1;
    slideTimer = setInterval("prepareSlide(" + itemToSlide + ")", 5000);
}
CaptainCarl
  • 3,411
  • 6
  • 38
  • 71

1 Answers1

2

You should provide a function to the first argument of setInterval. Also, to provide the context properly to the function invocation, a quick method is to preserve this by having another variable reference it, which in this case, we call self

var self = this;

slideTimer = setInterval(function(){
  self.prepareSlide(itemToSlide)
}, 5000);
Community
  • 1
  • 1
Joseph
  • 117,725
  • 30
  • 181
  • 234