This commit is contained in:
Ilya Kantor 2019-03-07 10:35:58 +03:00
parent 16cfa3037b
commit cdc1693443
7 changed files with 43 additions and 63 deletions

View file

@ -0,0 +1 @@
[js src="solution.view/extended-clock.js"]

View file

@ -1,9 +1,9 @@
class Clock {
constructor({ template }) {
this._template = template;
this.template = template;
}
_render() {
render() {
let date = new Date();
let hours = date.getHours();
@ -15,7 +15,7 @@ class Clock {
let secs = date.getSeconds();
if (secs < 10) secs = '0' + secs;
let output = this._template
let output = this.template
.replace('h', hours)
.replace('m', mins)
.replace('s', secs);
@ -24,11 +24,11 @@ class Clock {
}
stop() {
clearInterval(this._timer);
clearInterval(this.timer);
}
start() {
this._render();
this._timer = setInterval(() => this._render(), 1000);
this.render();
this.timer = setInterval(() => this.render(), 1000);
}
}

View file

@ -2,11 +2,11 @@ class ExtendedClock extends Clock {
constructor(options) {
super(options);
let { precision=1000 } = options;
this._precision = precision;
this.precision = precision;
}
start() {
this._render();
this._timer = setInterval(() => this._render(), this._precision);
this.render();
this.timer = setInterval(() => this.render(), this.precision);
}
};

View file

@ -1,23 +1,12 @@
<!DOCTYPE HTML>
<html>
<script src="clock.js"></script>
<script src="extended-clock.js"></script>
<head>
<title>Console clock</title>
</head>
<script>
let lowResolutionClock = new ExtendedClock({
template: 'h:m:s',
precision: 10000
});
<body>
<script src="clock.js"></script>
<script src="extended-clock.js"></script>
<script>
let lowResolutionClock = new ExtendedClock({
template: 'h:m:s',
precision: 10000
});
lowResolutionClock.start();
</script>
</body>
</html>
lowResolutionClock.start();
</script>

View file

@ -1,9 +1,9 @@
class Clock {
constructor({ template }) {
this._template = template;
this.template = template;
}
_render() {
render() {
let date = new Date();
let hours = date.getHours();
@ -15,7 +15,7 @@ class Clock {
let secs = date.getSeconds();
if (secs < 10) secs = '0' + secs;
let output = this._template
let output = this.template
.replace('h', hours)
.replace('m', mins)
.replace('s', secs);
@ -24,11 +24,11 @@ class Clock {
}
stop() {
clearInterval(this._timer);
clearInterval(this.timer);
}
start() {
this._render();
this._timer = setInterval(() => this._render(), 1000);
this.render();
this.timer = setInterval(() => this.render(), 1000);
}
}

View file

@ -1,34 +1,21 @@
<!DOCTYPE HTML>
<html>
<script src="clock.js"></script>
<script>
let clock = new Clock({
template: 'h:m:s'
});
clock.start();
<head>
<title>Console clock</title>
</head>
<body>
/* Your class should work like this: */
<!-- source clock -->
<script src="clock.js"></script>
<script>
let clock = new Clock({
template: 'h:m:s'
/*
let lowResolutionClock = new ExtendedClock({
template: 'h:m:s',
precision: 10000
});
clock.start();
/* Your class should work like this: */
/*
let lowResolutionClock = new ExtendedClock({
template: 'h:m:s',
precision: 10000
});
lowResolutionClock.start();
*/
</script>
</body>
</html>
lowResolutionClock.start();
*/
</script>

View file

@ -6,6 +6,9 @@ importance: 5
We've got a `Clock` class. As of now, it prints the time every second.
[js src="source.view/clock.js"]
Create a new class `ExtendedClock` that inherits from `Clock` and adds the parameter `precision` -- the number of `ms` between "ticks". Should be `1000` (1 second) by default.
- Your code should be in the file `extended-clock.js`