Home » Javascript » Javascript error at runtime – issue with scope or syntax? [on hold]

Javascript error at runtime – issue with scope or syntax? [on hold]

Posted by: admin August 19, 2018 Leave a comment

Questions:

Kinda new to javascript. I’m trying to do the following, but when o.carousel.setInterval(); and o.carousel.initializeCarousel(); are called, I’m getting an error in the console: Uncaught TypeError: Cannot read property 'setInterval' of undefined. I don’t understand why the browser thinks o.carousel is undefined. Any ideas?

(function (o) {

   var hasCarousel = $('#carousel').length > 0 ? true : false;
   if (hasCarousel) {
       o.carousel.setInterval();
       o.carousel.initializeCarousel();
   }
  ...

   o.carousel= {

    Interval: undefined,
    carouselArray: undefined,
    carouselArrayLength: undefined,

    setInterval: function () {
        console.log("set");
        Interval = setInterval(o.Carousel.processInterval, 3000)
    },

    stopInterval: function () {
        console.log("clear")
        clearInterval(Interval);
    },

    processInterval: function () {
        $("#carousel-controls #left-control").trigger("click");
    },
}
Answers: