Skip to content

Commit

Permalink
Create timer on model load to fix multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
bikingbadger committed Jun 2, 2020
1 parent b1de7da commit 62e7fd6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/modules/timer/controller.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ const TimerController = {
*/
startTimer: function () {
this.model.start();
// Use a faster timeout for local testing
const timeout =
window.location.hostname === 'localhost' ||
window.location.hostname === '127.0.0.1'
? 1
: 1000;
// // Use a faster timeout for local testing
// const timeout =
// window.location.hostname === 'localhost' ||
// window.location.hostname === '127.0.0.1'
// ? 1
// : 1000;
// Only start a new timer if one is not already running
if (!this.countDown === null) return;
this.countDown = window.setInterval(() => {
//Make sure the timer is running and not paused
if (this.model.running) {
// Check if the timer has ended, if it has determine the next course of action
if (this.model.currentTime <= 0) {
this.model.stop();
this.model.next();
} else {
/**
* Decrease the timer
*/
this.model.decrease();
}
}
// renderTimer();
}, timeout);
// if (!this.countDown === null) return;
// this.countDown = window.setInterval(() => {
// //Make sure the timer is running and not paused
// if (this.model.running) {
// // Check if the timer has ended, if it has determine the next course of action
// if (this.model.currentTime <= 0) {
// this.model.stop();
// this.model.next();
// } else {
// /**
// * Decrease the timer
// */
// this.model.decrease();
// }
// }
// // renderTimer();
// }, timeout);
},
/**
* Pause the timer
Expand Down
25 changes: 25 additions & 0 deletions src/modules/timer/model.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,38 @@ const TimerModel = {
pompoms: 0,
pubSub: null,
subject: 'timer',
countDown: null,
/**
* Load the object with all its default settings
*/
load: function (PubSub) {
// console.log(PubSub);
this.pubSub = PubSub;
this.publish();

// Use a faster timeout for local testing
const timeout =
window.location.hostname === 'localhost' ||
window.location.hostname === '127.0.0.1'
? 10
: 1000;

// create the timer once
this.countDown = window.setInterval(() => {
//Make sure the timer is running and not paused
if (this.running) {
// Check if the timer has ended, if it has determine the next course of action
if (this.currentTime <= 0) {
this.stop();
this.next();
} else {
/**
* Decrease the timer
*/
this.decrease();
}
}
}, timeout);
},
publish: function () {
this.pubSub.publish(this);
Expand Down

0 comments on commit 62e7fd6

Please sign in to comment.