我正在尝试设置图像交换器功能。到目前为止,这是我的代码:
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);
}
}
}
如您所见,这是一个带有两个参数的函数。然后它使用两种方法返回它自己的对象。当我在初始count
函数调用之后调用imageChanger
方法并将参数传递给url
时,它只会记录我传递的内容,然后运行setTimeout
函数时,未定义的后续时间。
我不确定我在这里做错了什么。为什么这个count
函数在第一个日志之后返回未定义?
答案 0 :(得分:1)
在setTimeout中,你应该使用self.count
参数而不是无参数来调用url
,即
self.count(url);