ساعت تع٠Û٠داد٠شدÙ
اÙÙ
ÛØª: 5
Ù
ا ÛÚ© Ú©ÙØ§Ø³ Clock(ساعت) دارÛÙ
. ÙÙ
اکÙÙÙØ ÙØ± ثاÙÛ٠را ÙÙ
Ø§ÛØ´ Ù
ÛâØ¯ÙØ¯.
class Clock {
constructor({ template }) {
this.template = template;
}
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 output = this.template
.replace('h', hours)
.replace('m', mins)
.replace('s', secs);
console.log(output);
}
stop() {
clearInterval(this.timer);
}
start() {
this.render();
this.timer = setInterval(() => this.render(), 1000);
}
}
ÛÚ© Ú©ÙØ§Ø³ Ø¬Ø¯ÛØ¯ ExtendedClock Ø¨Ø³Ø§Ø²ÛØ¯ ک٠از Clock Ø§Ø±Ø«âØ¨Ø±Û Ù
ÛâÚ©ÙØ¯ ٠پاراÙ
تر precision â ØªØ¹Ø¯Ø§Ø¯ ms بÛÙ ÙØ± «تÛÚ© تاک» â Ø±Ø§ اضاÙÙ Ù
ÛâÚ©ÙØ¯. Ø¨Ù Ø·ÙØ± Ù¾ÛØ´âÙØ±Ø¶ Ø¨Ø§ÛØ¯ 1000 (ÛÚ© ثاÙÛÙ) باشد.
- کد Ø´Ù
ا Ø¨Ø§ÛØ¯ در ÙØ§ÛÙ
extended-clock.jsباشد. - ÙØ§Û٠اصÙÛ
clock.jsرا تغÛÛØ± ÙØ¯ÙÛØ¯. آ٠را گسترش دÙÛØ¯.
class ExtendedClock extends Clock {
constructor(options) {
super(options);
let { precision = 1000 } = options;
this.precision = precision;
}
start() {
this.render();
this.timer = setInterval(() => this.render(), this.precision);
}
};