Skip to content

Commit

Permalink
Use ms for timings
Browse files Browse the repository at this point in the history
  • Loading branch information
hmellor committed Feb 7, 2022
1 parent 94d0ecc commit b4ca4fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ First, set `demoAuction=False` (this will keep the cats at bay).

Then, populate all of the arrays at the top of `js/auctions.js` with the information for of the items you'll be putting up for auction.

The only complicated option is `endTimes`, which is a list of times (relative to epoch) **in seconds**. See [JavaScript's `Date` class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for more information.
The only complicated option is `endTimes`, which is a list of times (relative to epoch) **in milliseconds**. See [JavaScript's `Date` class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for more information.

### Firebase setup
Here we will cover how to set up your Firebase project and then how to enable the Firebase authentication and database respectively.
Expand Down
20 changes: 10 additions & 10 deletions js/auctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let subtitles = [];
let details = [];
let secondaryImages = [];
let startingPrices = [55, 60, 20, 0, 4, 0, 99, 0, 12, 6, 3, 7];
let endTimes = [];
let endTimes = []; // Make sure to fix these to UTC time so they don't change with the users timezone

// Random auction information
function generateRandomAuctions() {
Expand Down Expand Up @@ -47,9 +47,9 @@ let startPrices = [];
for (let i = 0; i < startingPrices.length; i++) {
if (demoAuction) {
let now = new Date();
let endTime = new Date().setHours(8 + i, 0)
if (endTime - now < 0) { endTime = endTime + 86400 }
endTimes.push(endTime / 1000)
let endTime = new Date().setHours(8 + i, 0, 0, 0)
if (endTime - now < 0) { endTime = new Date(endTime).setDate(now.getDate() + 1) }
endTimes.push(endTime)
}
startPrices.push({
bid0: {
Expand All @@ -63,7 +63,7 @@ for (let i = 0; i < startingPrices.length; i++) {
// Convert time to string for HTML clocks
function timeBetween(start, end) {
let _string = ""
let secsRemaining = end - start;
let secsRemaining = (end - start) / 1000;
let d = parseInt(secsRemaining / 86400);
let h = parseInt(secsRemaining % 86400 / 3600);
let m = parseInt(secsRemaining % 3600 / 60);
Expand All @@ -77,15 +77,15 @@ function timeBetween(start, end) {

// Set time on HTML clocks
function setClocks() {
let nowTime = new Date().getTime() / 1000;
let now = new Date();
let nowTime = now.getTime();
for (i = 0; i < startingPrices.length; i++) {
timeLeft = timeBetween(nowTime, endTimes[i])
let timer = document.getElementById("time-left-" + i)
// remove finished auction after 5 minutes
if (endTimes[i] - nowTime < -300) {
document.getElementById("auction-" + i).parentElement.style.display = "none"
if (demoAuction) {
endTimes[i] = endTimes[i] + 86400 // add 1 day
endTimes[i] = new Date(endTimes[i]).setDate(now.getDate() + 1) // add 1 day
document.getElementById("auction-" + i).parentElement.remove()
resetLive(i);
resetStore(i);
Expand All @@ -98,15 +98,15 @@ function setClocks() {
timer.innerHTML = "Auction Complete";
document.getElementById("bid-button-" + i).setAttribute('disabled', '')
} else {
timer.innerHTML = timeLeft;
timer.innerHTML = timeBetween(nowTime, endTimes[i]);
}
}
setTimeout(setClocks, 1000);
}

// Place a bid on an item
function placeBid() {
let nowTime = new Date().getTime() / 1000;
let nowTime = new Date().getTime();
let modalBidButton = document.querySelector("#bid-modal > div > div > div.modal-footer > button.btn.btn-primary")
modalBidButton.setAttribute('disabled', '') // disable the button while we check
let i = modalBidButton.id.match("[0-9]+");
Expand Down

0 comments on commit b4ca4fc

Please sign in to comment.