gpt4 book ai didi

javascript - this.method 不适用于 setInterval

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:33 26 4
gpt4 key购买 nike

<分区>

我有这个简单的代码:

var Modules = (function() {
'use strict';

return {

TIMER: function (){
var timer = null;

return {
time: 100,
init: function() {

this.counter();
this.timer = window.setInterval(this.counter, 1000);
},
counter: function() {
this.time -= 1;

if (this.time <= 0) {
window.clearInterval(this.timer);
alert('Time expired');
}
console.log(this.time);
this.viewer();

},
viewer: function() {
document.getElementById('timer').innerHTML = this.time;
}
}
}
};
}());

Modules.TIMER().init();
<div id="timer"></div>

有些地方不对劲,因为我有 2 个错误:

this.viewer is not a function

NaN of this.time

我的设计模式在间隔上运行有什么问题?

延长TIMER后复位方法:

reset: function() {
this.time = 100;
}

并在外面称它为:Modules.TIMER().reset(); ?

我得到了

this.time is not defined

.

或者在初始化内部:

jQuery("body").on('keyup mouseup', function (e) {
this.reset();
});

我得到错误:

this.reset() is not s function.

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com