It's my first question here. I was looking for a similar topic but I didn't find the answer that I could use. I would like to get a timer in my memory game that displays current time (I want to use Date object). I used a function from here How to create an accurate timer in javascript? but after the first click on the card, the time does not begin from 0. It shows the time that has elapsed since the page loading.
//variable for timer
var gameScore = $(".score-game");
var timer = $("#timer");
gameScore.html("0 seconds");
var hasGameStarted = false;
var score = Date.now();
//timer function
function countdownTimer() {
if (hasGameStarted !== true) {
scoreTimeout = setInterval(function() {
var delta = Date.now() - score;
gameScore.html(Math.floor(delta / 1000) + " seconds");
}, 1000);
hasGameStarted = true;
}
}
cards.on('click', function() {
countdownTimer();
});
If you want to see how works my game, check this site (timer is working good now but does not display time from date object):
https://borkson.github.io/Cars-Memory-Game/
Sorry for my English :(