From b4ca4fcfb06213394b63ae5473c986b620a0cf1f Mon Sep 17 00:00:00 2001 From: HMellor Date: Mon, 7 Feb 2022 09:20:56 +0000 Subject: [PATCH] Use ms for timings --- README.md | 2 +- js/auctions.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f1c3ed55..93c09c33 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/js/auctions.js b/js/auctions.js index cc4f8249..f3c75a8a 100644 --- a/js/auctions.js +++ b/js/auctions.js @@ -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() { @@ -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: { @@ -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); @@ -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); @@ -98,7 +98,7 @@ 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); @@ -106,7 +106,7 @@ function setClocks() { // 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]+");