JavaScript this.method不是函数

时间:2019-10-10 12:04:34

标签: javascript function oop scope this

学习Javascript,我确实对此和/或范围的概念有疑问。因此,我有一个用于计时器的以下代码,可将自己插入页脚:

var button = document.getElementById("btnSign");
var timerText = document.getElementById("timer");
var timerZone = document.getElementById("timerZone");

button.addEventListener("click", function(e) {
  event.preventDefault();
  document.getElementById("timerZone").style.display = "block"; // display of timer bloc
  new timer()
})

class timer {
  constructor() {
    this.timerText = document.getElementById("timer");
    this.counter = 0;
    this.timeLeft = 1200

    this.countDown();
    setInterval(this.countDown, 1000)
  } // end of constructor

  //class methods
  convertSec(s) {
    this.min = Math.floor(s / 60);
    this.sec = s % 60;
    return this.min + ":" + this.sec;
  }

  countDown() {
    this.counter++;
    this.timerText.innerHTML = (this.convertSec(this.timeleft - this.counter))
  }
}
<button id="btnSign">Sign</button>
<div id="timerZone">
  <span id="timer"></span>
</div>

//计时器类结束

控制台返回以下错误:

  

this.convertSec不是函数

我想这与范围超出范围有关,但是我不确定自己在做什么错吗?

谢谢!

0 个答案:

没有答案