-3

I would like to have a countdown timer always show a countdown for every new user. Basically, if I close the webpage, and reopen it, the timer should still be running. I'm thinking of using the JS variable code functions to define a new client's timezone together with an if statement comment and make it a repeat loop?

Basically, I would want to run a timer on the server side, not the client side.

Has anyone done this before?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Zoe Lee
  • 7
  • 1
  • 2
    Welcome to StackOverflow. Can you provide some code? This will serve as a starting point to answer your question – molamk Feb 09 '19 at 20:05
  • Did you want something like this fiddle: https://jsfiddle.net/32v0sm8w/1/? Or did you want the timer to run server side? – Aniket G Feb 09 '19 at 20:43
  • Javascript it a client side language, and will only run when you open the page. This link might help you: https://stackoverflow.com/questions/21399483/php-server-side-timer – Aniket G Feb 09 '19 at 20:48

1 Answers1

0

Sounds something that you could try to solve with browsers localStorage. If your only requirement is to keep saved time available after you close tab/browser and come back to your page, this is a good fit for you.

Here's a small Codesandbox example code of how to conditionally check and save to localStorage and then start counter based on that value.

https://codesandbox.io/s/4xw97q02m0

EDIT: same code pasted to a post

function setCurrentTime(){
  let localStore = window.localStorage.getItem("myTime") // Check if already exists
  if(localStore) return localStore
  const time = Date.now(); // get current time stamp
  window.localStorage.setItem("myTime", time) // Save to localStorage
  return time
}

function startCount(){
  const time = setCurrentTime()
  const elem = document.getElementById("app")

  setInterval(() => {
    elem.innerHTML = `seconds since time saved: 
     ${Math.floor((Date.now() - time) / 1000)}
    `
  }, 1000)
}
startCount()
<div id="app"></div>
Jimi Pajala
  • 2,358
  • 11
  • 20