This repository was archived by the owner on Aug 12, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +56
-5
lines changed
Expand file tree Collapse file tree 3 files changed +56
-5
lines changed Original file line number Diff line number Diff line change 77 </div >
88 <div class =" player-controls grid" >
99 <div class =" decrement" >
10- <button class =" btn" @click.prevent =" decrement" >-</button >
10+ <button class =" btn" @click.prevent =" decrement" :disabled = " !isRunning " >-</button >
1111 </div >
1212 <span class =" score" >{{ score }}</span >
1313 <div class =" increment" >
14- <button class =" btn" @click.prevent =" increment" >+</button >
14+ <button class =" btn" @click.prevent =" increment" :disabled = " !isRunning " >+</button >
1515 </div >
1616 </div >
1717 </div >
@@ -48,6 +48,7 @@ export default {
4848 computed: {
4949 ... mapGetters ([
5050 ' highestScore' ,
51+ ' isRunning' ,
5152 ]),
5253 isHighest () {
5354 return this .highestScore === this .score ;
Original file line number Diff line number Diff line change 11<template >
22 <div class =" stopwatch" >
33 <h2 class =" title" >Stopwatch</h2 >
4- <span class =" stopwatch-time" >7 </span >
4+ <span class =" stopwatch-time" >{{ seconds }} </span >
55 <div class =" stopwatch-controls" >
6- <button class =" btn handler" >Start</button >
7- <button class =" btn" >Reset</button >
6+ <button class =" btn handler" @click.prevent =" toogleHandler" >
7+ {{ isRunning ? 'Stop' : 'Start' }}
8+ </button >
9+ <button class =" btn" @click.prevent =" reset" >Reset</button >
810 </div >
911 </div >
1012</template >
1113
1214<script >
15+ import { mapGetters } from ' vuex' ;
16+
1317export default {
1418 name: ' Stopwatch' ,
19+ data () {
20+ return {
21+ elapsedTime: 0 ,
22+ previousTime: 0 ,
23+ intervalId: null ,
24+ };
25+ },
26+ mounted () {
27+ this .intervalId = setInterval (() => this .tick (), 100 );
28+ },
29+ beforeDestroy () {
30+ clearInterval (this .intervalId );
31+ },
32+ methods: {
33+ toogleHandler () {
34+ const status = this .isRunning ;
35+ this .$store .dispatch (' STATUS_GAME' , ! status);
36+ this .previousTime = Date .now ();
37+ },
38+ tick () {
39+ if (this .isRunning ) {
40+ const now = Date .now ();
41+ const time = this .elapsedTime ;
42+ this .elapsedTime = time + (now - this .previousTime );
43+ this .previousTime = now;
44+ }
45+ },
46+ reset () {
47+ this .elapsedTime = 0 ;
48+ },
49+ },
50+ computed: {
51+ ... mapGetters ([
52+ ' isRunning' ,
53+ ]),
54+ seconds () {
55+ return Math .floor (this .elapsedTime / 1000 );
56+ },
57+ },
1558};
1659 </script >
Original file line number Diff line number Diff line change @@ -27,6 +27,9 @@ export default new Vuex.Store({
2727 REMOVE_POINT : ( { commit } , { playerIndex } ) => {
2828 commit ( 'DECREMENT' , playerIndex ) ;
2929 } ,
30+ STATUS_GAME : ( { commit } , status ) => {
31+ commit ( 'STATUS_GAME' , status ) ;
32+ } ,
3033 } ,
3134 mutations : {
3235 SET_PLAYER : ( state , { name } ) => {
@@ -44,6 +47,9 @@ export default new Vuex.Store({
4447 state . players [ index ] . points -= 1 ;
4548 }
4649 } ,
50+ STATUS_GAME : ( state , status ) => {
51+ state . isRunning = status ;
52+ } ,
4753 } ,
4854 getters : {
4955 players : state => state . players ,
@@ -57,5 +63,6 @@ export default new Vuex.Store({
5763 }
5864 return null ;
5965 } ,
66+ isRunning : state => state . isRunning ,
6067 } ,
6168} ) ;
You can’t perform that action at this time.
0 commit comments