我正在尝试在我的 js 类中创建间隔,代码如下所示:
class Clock {
constructor(template) {
this.template = template;
this.timer = this.timer;
this.date = new Date(); // call date
}
render = () => {
let hours = this.date.getHours(); // get hours
if (hours < 10) hours = '0' + hours;
let minutes = this.date.getMinutes(); // get minutes
if (minutes < 10) minutes = '0' + minutes;
let seconds = this.date.getSeconds(); // get seconds
if (seconds < 10) seconds = '0' + seconds;
let output = hours + ':' + minutes + ':' + seconds;
return output; // output
}
stop = () => { // stop interval
clearInterval(this.timer);
}
start = () => { // start interval
this.timer = setInterval(this.render(), 1000);
}
}
var clock = new Clock({template: ''});
clock.start();
我有这样的错误:
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received '06:16:49'
at setInterval (timers.js:166:3)
at Clock.start (/home/runner/Training/index.js:28:20)
at /home/runner/Training/index.js:33:7
at Script.runInContext (vm.js:130:18)
at Object.<anonymous> (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
我认为问题是当我打电话this.timer = setInterval(this.render(), 1000);但是当我更改为错误this.timer = setInterval(this.render, 1000);时会显示这样javascript。所以任何人都可以帮助我。谢谢。
慕尼黑5688855
沧海一幻觉
动漫人物
德玛西亚99
相关分类