Reputation: 984
I'd like to create an object with the key set to the time a button was clicked. I'd then like to check every second the current time versus the time created and console.log() the relative time. The problem being it never changes from 'A few seconds ago'. What am I missing?
Codepen Live Example: http://codepen.io/colealanroberts/pen/VLvpzR/?editors=001
Here's what I have thus far:
(function() {
// HTML Elements
var $btn = document.getElementById('save');
var $relTime = document.getElementById('relative-time');
var tObj = {};
var now = moment();
var t = now.format("dddd, MMMM Do YYYY, h:mm:ss a");
function saveObj() {
tObj = {
timeClicked: t
}
// Log initial time
console.log(tObj.timeClicked);
setInterval(function() {
var rel = moment([tObj.timeClicked]).fromNow(true);
console.log(rel);
}, 1000);
}
$btn.addEventListener('click', saveObj);
})();
Upvotes: 0
Views: 1303
Reputation: 382102
You don't save the moment but a string.
If you want to keep using that string, you must parse it:
var rel = moment(tObj.timeClicked,["dddd, MMMM Do YYYY, h:mm:ss a"]).fromNow(true);
Of course it would be simpler to store the moment itself : http://codepen.io/anon/pen/qdOjqr
Upvotes: 1