Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript原型中定义重复函数?_Javascript_Prototype_Settimeout - Fatal编程技术网

如何在Javascript原型中定义重复函数?

如何在Javascript原型中定义重复函数?,javascript,prototype,settimeout,Javascript,Prototype,Settimeout,我试图定义一个带有重复函数的Javascript类,但无法使其正常工作: var Repeater = function() { this.init.apply(this, arguments); }; Repeater.prototype = { run: 0, // how many runs interval: 5, // seconds init: function() { this.repeat(); }, repe

我试图定义一个带有重复函数的Javascript类,但无法使其正常工作:

var Repeater = function() {
    this.init.apply(this, arguments);
};

Repeater.prototype = {
    run: 0, // how many runs
    interval: 5, // seconds

    init: function() {
        this.repeat();
    },

    repeat: function() {
        console.log(++this.run);
        setTimeout(this.repeat, this.interval * 1000);
    }
};

var repeater = new Repeater();
应该如何做到这一点?

试着这样做:

var Repeater = function() {
    this.init.apply(this, arguments);
};

Repeater.prototype = {
    run: 0, // how many runs
    interval: 5, // seconds

    init: function() {
        this.repeat();
    },

    repeat: function() {
        console.log(++this.run);
        var that = this;
        setTimeout(function() {that.repeat()}, this.interval * 1000);
    }
};

var repeater = new Repeater();
在这个问题中,您可以阅读更多关于此行为的信息:

尝试这样做:

var Repeater = function() {
    this.init.apply(this, arguments);
};

Repeater.prototype = {
    run: 0, // how many runs
    interval: 5, // seconds

    init: function() {
        this.repeat();
    },

    repeat: function() {
        console.log(++this.run);
        var that = this;
        setTimeout(function() {that.repeat()}, this.interval * 1000);
    }
};

var repeater = new Repeater();
在这个问题中,您可以阅读有关此行为的更多信息:

尝试此代码:

var Repeater = function() {
    this.run = 0;  // how many runs
    this.interval = 5; // seconds
    this.init.apply(this, arguments);
};

Repeater.prototype.init = function() {
    this.repeat();
}

Repeater.prototype.repeat = function() {
    var _this = this;
    console.log(++this.run);
    setTimeout(function () { _this.repeat() }, this.interval * 1000);
};

var repeater = new Repeater();
我已经将run和interval移到了构造函数中,因为如果您将它添加到原型中,那么它将分布到所有实例中

您的问题在于
seTimeout
-在您的代码中,此计时器为
中继器设置了新的作用域,并且
不再指向
中继器
实例,而是指向
超时
实例。您需要缓存
(我已调用此缓存
\u this
),并将其调用到传递给
setTimeout
的新函数中。请尝试以下代码:

var Repeater = function() {
    this.run = 0;  // how many runs
    this.interval = 5; // seconds
    this.init.apply(this, arguments);
};

Repeater.prototype.init = function() {
    this.repeat();
}

Repeater.prototype.repeat = function() {
    var _this = this;
    console.log(++this.run);
    setTimeout(function () { _this.repeat() }, this.interval * 1000);
};

var repeater = new Repeater();
我已经将run和interval移到了构造函数中,因为如果您将它添加到原型中,那么它将分布到所有实例中


您的问题在于
seTimeout
-在您的代码中,此计时器为
中继器设置了新的作用域,并且
不再指向
中继器
实例,而是指向
超时
实例。您需要缓存
this
(我已调用此缓存
\u this
),并将其调用到传递给
setTimeout
的新函数中。更改repeat函数以在setTimeout调用中使用闭包,如下所示:

repeat: function() {
var ctx = this;
    console.log(++this.run);
    setTimeout(function(){ctx.repeat()}, this.interval * 1000);
}

在这些场景中,您需要显式地设置上下文-这就是ctx变量的作用

更改repeat函数以在setTimeout调用中使用闭包,如下所示:

repeat: function() {
var ctx = this;
    console.log(++this.run);
    setTimeout(function(){ctx.repeat()}, this.interval * 1000);
}


在这些场景中,您需要明确地设置上下文-这就是ctx变量的作用

这与OP所做的有什么不同?(之后缺少括号。repeat())@Adam
setTimeout
中的
this
值不是您期望的值。设置
var=this在它之前,使用它将确保它引用了正确的对象。我看到它是有效的,但我认为你应该详细说明你的答案。this.repeat inside setTimeout确实调用了正确的函数,但它没有使用正确的范围调用它。setTimeout在全局范围内执行它的函数参数,这就是这里发生的事情。谢谢你帮我理解这一点
但我认为你应该详细说明你的答案
-我完全同意。只有代码的答案并没有真正的帮助我添加了一个链接到另一个问题来解释它,因为我觉得这个被广泛误解了,实际上我会重复另一个问题中给出的答案。否则,我同意你们两个的观点——我首先应该详细阐述;)这与OP所做的有什么不同?(之后缺少括号。repeat())@Adam
setTimeout
中的
this
值不是您期望的值。设置
var=this在它之前,使用它将确保它引用了正确的对象。我看到它是有效的,但我认为你应该详细说明你的答案。this.repeat inside setTimeout确实调用了正确的函数,但它没有使用正确的范围调用它。setTimeout在全局范围内执行它的函数参数,这就是这里发生的事情。谢谢你帮我理解这一点
但我认为你应该详细说明你的答案
-我完全同意。只有代码的答案并没有真正的帮助我添加了一个链接到另一个问题来解释它,因为我觉得这个被广泛误解了,实际上我会重复另一个问题中给出的答案。否则,我同意你们两个的观点——我首先应该详细阐述;)你真的想让所有实例共享
运行
间隔
?我觉得一个简单的函数就足够了如果它很重要,下面是我如何设置它的:。我显然不确定这是如何使用的,但我就是这样解释的it@Ian谢谢,这只是一个例子;实际上,我还有很多其他的东西,为了简单起见,我省略了。你真的想让所有实例共享
run
interval
?@Ian我把它作为一个单例使用,但你的观点是有效的如果它是作为一个单例使用的,为什么你要使用构造函数方法?我觉得一个简单的函数就足够了如果它很重要,下面是我如何设置它的:。我显然不确定这是如何使用的,但我就是这样解释的it@Ian谢谢,这只是一个例子;事实上,我还有很多其他的事情要做,为了简单起见,我把它们省略了。