简体   繁体   English

无法控制台记录我的方法插入 DOM 的 HTML

[英]Unable to console.log HTML that my method inserts into DOM

I'm making a simple clock app that displays the current time as a way of practicing my JS in combination with HTML & CSS (I'm quite new to coding).我正在制作一个简单的时钟应用程序,它显示当前时间,作为结合 HTML 和 CSS 练习我的 JS 的一种方式(我对编码很陌生)。

My code works fine, but I'm confused about an unexpected result I got when experimenting.我的代码运行良好,但我对实验时得到的意外结果感到困惑。 I created a class, instantiated it, and then tried to print the inner HTML that my display() method was inserting into a pre-existing div (id="time").我创建了一个 class,对其进行实例化,然后尝试打印我的 display() 方法插入到预先存在的 div (id="time") 中的内部 HTML。

See below:见下文:

 class Clock { constructor() { this.date = new Date; this.day = this.date.getDate(); this.secs = this.date.getSeconds(); this.mins = this.date.getMinutes(); this.hours = this.date.getHours(); } update(increment) { this.secs += increment; if (this.secs >= 60) { this.secs = this.secs - 60; this.mins++; } if (this.mins >= 60) { this.mins = 0; this.hours++; } if (this.hours >= 24) { this.hours = 0; } } display() { this.update(1); let HMS = [this.hours, this.mins, this.secs].map(item => item < 10? `0${item}`: item); document.getElementById('time').innerHTML = `${HMS[0]}: ${HMS[1]}: ${HMS[2]}`; } run() { setInterval(this.display.bind(this), 1000); } } // TESTING let clock = new Clock; // Create new clock instance clock.run(); // Clock is running let time = document.getElementById('time'); // Retrieving element that contains the time console.log(time.innerHTML);
 <div id="time"></div>

Gives empty string?给出空字符串? Why does it not log the HTML I inserted in my display() method?为什么它不记录我在 display() 方法中插入的 HTML?

As above, I don't understand why the HTML that's inserted into my 'time' div as part of the display() method isn't recognized when I console.log it.如上所述,我不明白为什么当我 console.log 时无法识别作为 display() 方法的一部分插入到我的“时间”div 中的 HTML。 I feel there's a concept that I'm not grasping in this example, and I can't figure out what it is.我觉得这个例子中有一个我没有掌握的概念,我无法弄清楚它是什么。

Could someone please help explain it?有人可以帮忙解释一下吗?

Many thanks!非常感谢!

setInterval does not fire immediately, it will only start firing after the first delay (1 second). setInterval不会立即触发,它只会在第一个延迟(1 秒)后开始触发。

As such at the moment your console.log fires before there is anything within the time div.因此,此时您的console.log在时间 div 内没有任何内容之前触发。

One way around this is to call this.display and then set the setInterval .解决此问题的一种方法是调用this.display然后设置setInterval

Example to demonstrate the current problem演示当前问题的示例

In this example I have added some text inside the time div, you will see when you run the example that text displays for a second and is also logged to the console.在此示例中,我在时间 div 中添加了一些文本,当您运行示例时,您会看到该文本显示一秒钟,并且还会记录到控制台。

I then log the time div contents 1.5 seconds later and it then shows the current time.然后我在 1.5 秒后记录时间 div 内容,然后显示当前时间。

 class Clock { constructor() { this.date = new Date; this.day = this.date.getDate(); this.secs = this.date.getSeconds(); this.mins = this.date.getMinutes(); this.hours = this.date.getHours(); } update(increment) { this.secs += increment; if (this.secs >= 60) { this.secs = this.secs - 60; this.mins++; } if (this.mins >= 60) { this.mins = 0; this.hours++; } if (this.hours >= 24) { this.hours = 0; } } display() { this.update(1); let HMS = [this.hours, this.mins, this.secs].map(item => item < 10? `0${item}`: item); document.getElementById('time').innerHTML = `${HMS[0]}: ${HMS[1]}: ${HMS[2]}`; } run() { setInterval(this.display.bind(this), 1000); } } // TESTING let clock = new Clock; // Create new clock instance clock.run(); // Clock is running let time = document.getElementById('time'); // Retrieving element that contains the time console.log(time.innerHTML); setTimeout(function(){ console.log(time.innerHTML); }, 1500);
 <div id="time">Not Updated Yet</div>

Example to demonstrate for to fix the issue演示解决问题的示例

In this example all I do is call the display() function immediately within your run function and then set up the setInterval .在这个例子中,我所做的只是在你的run function 中立即调用display() function 然后设置setInterval This way the time is set immediately and so can be logged to the console.这样,时间会立即设置,因此可以记录到控制台。

 class Clock { constructor() { this.date = new Date; this.day = this.date.getDate(); this.secs = this.date.getSeconds(); this.mins = this.date.getMinutes(); this.hours = this.date.getHours(); } update(increment) { this.secs += increment; if (this.secs >= 60) { this.secs = this.secs - 60; this.mins++; } if (this.mins >= 60) { this.mins = 0; this.hours++; } if (this.hours >= 24) { this.hours = 0; } } display() { this.update(1); let HMS = [this.hours, this.mins, this.secs].map(item => item < 10? `0${item}`: item); document.getElementById('time').innerHTML = `${HMS[0]}: ${HMS[1]}: ${HMS[2]}`; } run() { this.display(); setInterval(this.display.bind(this), 1000); } } // TESTING let clock = new Clock; // Create new clock instance clock.run(); // Clock is running let time = document.getElementById('time'); // Retrieving element that contains the time console.log(time.innerHTML);
 <div id="time"></div>

 class Clock { constructor() { this.date = new Date; this.day = this.date.getDate(); this.secs = this.date.getSeconds(); this.mins = this.date.getMinutes(); this.hours = this.date.getHours(); } update(increment) { this.secs += increment; if (this.secs >= 60) { this.secs = this.secs - 60; this.mins++; } if (this.mins >= 60) { this.mins = 0; this.hours++; } if (this.hours >= 24) { this.hours = 0; } } display() { this.update(1); let HMS = [this.hours, this.mins, this.secs].map(item => item < 10? `0${item}`: item); document.getElementById('time').innerHTML = `${HMS[0]}: ${HMS[1]}: ${HMS[2]}`; } run() { setInterval(this.display.bind(this), 1000); } } // TESTING let clock = new Clock; // Create new clock instance clock.run(); // Clock is running let time = document.getElementById('time'); // Retrieving element that contains the time console.log(time);
 <div id="time"></div>

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

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