This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make game start countdown timer count down
Rather than being a static number updated on page refresh, the countdown clock will not update itself. This makes for a more interesting display. Using time remaining rather than an absolute time to prevent jittery behaviour at the start, though at the expense of accuracy.
- Loading branch information
1 parent
eb06ec4
commit 9bbc30c
Showing
3 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// @flow | ||
|
||
var $ = require('jquery'); | ||
|
||
function noCountdown(container) { | ||
$('.countdown-days', container).text('--'); | ||
$('.countdown-hours', container).text('--'); | ||
$('.countdown-minutes', container).text('--'); | ||
$('.countdown-seconds', container).text('--'); | ||
} | ||
|
||
module.exports = { | ||
_container: null, | ||
_remaining: 0, | ||
_timeDiff: 0, | ||
_interval: null, | ||
|
||
startCountdown: function() { | ||
this._container = $('.upcoming-game-countdown'); | ||
if( !this._container.length ) { | ||
return; | ||
} | ||
|
||
this._remaining = parseInt(this._container.data().remaining); | ||
|
||
if(!this._container.length || !this._remaining) { | ||
return noCountdown(this._container); | ||
} | ||
|
||
var self = this; | ||
this._interval = setInterval( | ||
function() { | ||
self.setTimeRemaining() | ||
}, | ||
1000 | ||
); | ||
}, | ||
|
||
setTimeRemaining: function setTimeRemaining() { | ||
this._remaining -= 1; | ||
var secs = this._remaining; | ||
|
||
if( secs < 0 ) { | ||
noCountdown(this._container); | ||
if(this._interval) { | ||
clearInterval(this._interval); | ||
} | ||
return; | ||
} | ||
|
||
var days = parseInt((secs/(60*60*24)) % 24); | ||
var hours = parseInt((secs/(60*60)) % 24); | ||
var minutes = parseInt((secs/60) % 60); | ||
var seconds = parseInt(secs % 60); | ||
|
||
$('.countdown-days', this._container).text(days); | ||
$('.countdown-hours', this._container).text(hours); | ||
$('.countdown-minutes', this._container).text(minutes); | ||
$('.countdown-seconds', this._container).text(seconds); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters