简体   繁体   English

该元素不会接受 textContent 输入

[英]The element will not take a textContent input

When I click the date value I'm having this error: Uncaught TypeError: Cannot read property 'textContent' of undefined当我单击日期值时出现此错误:未捕获的类型错误: Uncaught TypeError: Cannot read property 'textContent' of undefined

This is my code can you help me in determining the source of the error and how can I correct this?这是我的代码,你能帮我确定错误的来源吗?我该如何纠正?

function Timer(elem) {
  var time = 3000;
  var interval;
  var offset;

  function update() {`enter code here`
    time += delta();
    var formattedTime = timeFormatter(time);
    elem.textContent = formattedTime;
  }

  function delta() {
    var now = Date.now();
    var timePassed = now - offset;
    offset = now;
    return timePassed;
  }

  function timeFormatter(timeInMilliseconds) {
    var time = new Date(timeInMilliseconds)
    var minutes = time.getMinutes().toString();
    var seconds = time.getSeconds().toString();

    if (minutes.length < 2) {
      minutes = '0' + minutes;
    }
    if (seconds.length < 2) {
      seconds = '0' + seconds;
    }

    return minutes + ' : ' + seconds;
  }

  this.isOn = false;
  this.start = function() {};
  if (!this.isOn) {
    interval = setInterval(update, 10);
    offset = Date.now();
    this.isOn = true;
  }
};  

this.stop = function() {
  if (this.isOn) {
    clearInterval(interval);
    interval = nul;
    this.isOn = false;
  } 
};

this.reset = function() {};

Edited version for the other errors.其他错误的编辑版本。

function Timer(elem) {
var time= 0;
var offset;
var interval;

  function update() {
    if (this.isOn) {
      time += delta();
      var formattedTime = timeFormatter(time);
    }
    
    elem.textContent = formattedTime;
  }

function delta() {
    var now =  Date.now();
    var timePassed = now - offset;

    offset = "5:00";

    return timePassed;
}

  function timeFormatter(time) {
    time = new Date(time);

    var minutes = time.getMinutes().toString();
    var seconds = time.getSeconds().toString();

    if (minutes.length < 2) {
      minutes = '0' + minutes;
    }

    if (seconds.length < 2) {
      seconds = '0' + seconds;
    }

    return minutes + ' : ' + seconds;
  }

this.start = function() {
    interval = setInterval(update.bind(this), 10);
    time++;
    this.isOn = true;
};

this.stop = function() {
    clearInterval(interval)
    interval = null;
    this.isOn = false;
};

this.reset = function() {
    time= 300;
    update();
};

this.isOn = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM