My first class example
- class Clock {
- constructor({ template,style }) {
- this._template = template;
- this._class = style;
- }
-
- _createElement(heure) {
-
- let node = document.createElement("div");
- console.log(this._class);
- node.classList.add(this._class);
-
- document.body.appendChild(node);
-
- this._node = node;
-
- }
-
- _render() {
- let date = new Date();
-
- let hours = date.getHours();
- if (hours < 10) hours = '0' + hours;
-
- let mins = date.getMinutes();
- if (mins < 10) mins = '0' + mins;
-
- let secs = date.getSeconds();
- if (secs < 10) secs = '0' + secs;
-
- let l = date.getMilliseconds();
- if (l < 10) {
- l = '00' + l;
- } else if (l < 100) {
- l = '0' + l;
- }
-
-
- let output = this._template
- .replace(/H/ig, hours)
- .replace('m', mins)
- .replace('s', secs)
- .replace('l', l);
-
- this._node.innerText = output;
- }
-
- stop() {
- clearInterval(this._timer);
- }
-
- start() {
- this._createElement();
- this._render();
- this._timer = setInterval(() => this._render(), 10);
- }
- }
-
- let tabC = [
- new Clock({template : "⏰ h:m:s:l", style:"hms"}),
- new Clock({template : "Il est : s->m->h", style:"smh"}),
- new Clock({template : "l", style:"l"}),
- ];
-
-
- for (let c of tabC){
- c.start();
- }