JSFiddle - React, Tailwind, and code Playground
by Mason Cooper
HTML
<p id="count"></p>
JavaScript
var currentTime;
// check and see if counter has started before
if (localStorage['time']) { // will return null if not previously used (and evaluate to false)
currentTime = Number.parseFloat(localStorage['time']); // will set current time to old time
// parseFloat will parse the value as a float, so we can do math (add)!
} else {
currentTime = 0; // if we haven't seen the user yet, we'll set them to 0 to start off
}
var container = document.getElementById('count'); // lets grab our html tag for display
// set interval takes two arguments, a function and a time (in miliseconds)
setInterval(function() {
currentTime += .1; // increment the seconds count by 1/10th of a second, or 100 miliseconds
currentTime = Math.round(currentTime*10)/10; // for formatting, will keep the Number to 1 decimal place
container.innerHTML = currentTime; // change our HTML item's innards
localStorage['time'] = currentTime; // store the time so we can pull it later
}, 100); // run the function every 100 miliseconds (1/10 a second)