I am trying to set up an image swapper function. Here is my code so far:
var imageChanger = function(start, end) {
var start = 1;
var end = 22;
return {
count: function(url) {
var self = this;
if(start > end) {
start = 1;
}
console.log(url);
console.log(start++);
imageSwapper = setTimeout( function() {
self.count();
}, 2000)
},
stopCount: function() {
clearTimeout(imageSwapper);
}
}
}
As you can see, this is a function that takes two parameters. It then returns an object of it’s own with two methods. When I call the count method after the initial imageChanger function call and pass a parameter to url it only logs what I pass one time and then when the setTimeout function runs, undefined subsequent times.
I am not sure what I am doing wrong here. Why is this count function returning undefined after the first log??
In the setTimeout, you should call
self.countwith theurlargument instead of no-argument, i.e.