函数在第一次登录后返回undefined

时间:2013-01-16 04:12:47

标签: javascript

我正在尝试设置图像交换器功能。到目前为止,这是我的代码:

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函数在第一个日志之后返回未定义?

1 个答案:

答案 0 :(得分:1)

在setTimeout中,你应该使用self.count参数而不是无参数来调用url,即

self.count(url);
相关问题