第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

類中調(diào)用間隔時(shí)的錯(cuò)誤回調(diào)

類中調(diào)用間隔時(shí)的錯(cuò)誤回調(diào)

哈士奇WWW 2023-04-27 10:08:03
我正在嘗試在我的 js 類中創(chuàng)建間隔,代碼如下所示: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();我有這樣的錯(cuò)誤: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:7at 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)我認(rèn)為問題是當(dāng)我打電話this.timer = setInterval(this.render(), 1000);但是當(dāng)我更改為錯(cuò)誤this.timer = setInterval(this.render, 1000);時(shí)會(huì)顯示這樣javascript。所以任何人都可以幫助我。謝謝。
查看完整描述

4 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

您的代碼存在一些問題,但請嘗試以下操作:


class Clock {

  constructor(template) {

    this.template = template;

    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);

  }

}


const clock = new Clock("");

clock.start();

您的構(gòu)造函數(shù)設(shè)置不正確(但也未使用)并且您的this引用已關(guān)閉。


實(shí)時(shí)示例: https ://codesandbox.io/s/clock-test-1r8dm?file=/src/index.js


根據(jù) OP 的評論,OP 可能要求增加時(shí)間。如果是這樣,請將渲染替換為


  render() {

    const nD = new Date();

    let hours = nD.getHours(); // get hours

    if (hours < 10) hours = "0" + hours;

    let minutes = nD.getMinutes(); // get minutes

    if (minutes < 10) minutes = "0" + minutes;

    let seconds = nD.getSeconds(); // get seconds

    if (seconds < 10) seconds = "0" + seconds;

    let output = hours + ":" + minutes + ":" + seconds;

    return output; // output

  }


查看完整回答
反對 回復(fù) 2023-04-27
?
滄海一幻覺

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊

setInterval 接受函數(shù)的名稱


class Clock {

  constructor(template) {

    this.template = template;

    this.timer = this.timer;

    this.date = new Date();

  }


  render = () =>  {

    let time = this.date;

    let hours = time.getHours();

    if (hours < 10) hours = "0" + hours;

    let minutes = time.getMinutes();

    if (minutes < 10) minutes = "0" + minutes;

    let seconds = time.getSeconds();

    if (seconds < 10) seconds = "0" + seconds;


    let output = hours + ":" + minutes + ":" + seconds;


    console.log(output);

  }


  stop = () => {

    clearInterval(this.timer);

  }


  start =() => {

    this.timer = setInterval(this.render, 1000);

  }

}


var clock = new Clock({ template: "" });

clock.start();


查看完整回答
反對 回復(fù) 2023-04-27
?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

setInterval需要一個(gè)回調(diào)函數(shù)。你不應(yīng)該執(zhí)行它。


start =() => {

     this.timer = setInterval(() => this.render(), 1000);

   }


class Clock {


  constructor(template) {

    this.template = template;

    this.timer = this.timer;

   // this.date = new Date(); // call date

  }


  render = () => {

    const date = new Date();

    let hours = date.getHours(); // get hours

    if (hours < 10) hours = '0' + hours;

    let minutes = date.getMinutes(); // get minutes

    if (minutes < 10) minutes = '0' + minutes;

    let seconds = date.getSeconds(); // get seconds

    if (seconds < 10) seconds = '0' + seconds;


    let output = hours + ':' + minutes + ':' + seconds;


    console.log(output)


    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();


查看完整回答
反對 回復(fù) 2023-04-27
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

setInterval 創(chuàng)建了一個(gè)新范圍,所以this在 setInterval 內(nèi)部不是同一個(gè)類實(shí)例,所以this.date.getHours()會(huì)拋出錯(cuò)誤,因?yàn)?code>this.datewill undefined。



查看完整回答
反對 回復(fù) 2023-04-27
  • 4 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號