-1

I'm trying to dynamically update the time stamp 1 minute ago using the following JavaScript:

setInterval('relativeTime()', 1000);

function relativeTime()
{
    console.log(timeSince('2015-09-17 14:59:10'));
}


function timeSince(date) {
    if (typeof date !== 'object') {
        date = new Date(date);
    }

    var seconds = Math.floor((new Date() - date) / 1000);
    var intervalType;

    var interval = Math.floor(seconds / 31536000);
    if (interval >= 1) {
        intervalType = 'year';
    } else {
        interval = Math.floor(seconds / 2592000);
        if (interval >= 1) {
            intervalType = 'month';
        } else {
            interval = Math.floor(seconds / 86400);
            if (interval >= 1) {
                intervalType = 'day';
            } else {
                interval = Math.floor(seconds / 3600);
                if (interval >= 1) {
                    intervalType = "hour";
                } else {
                    interval = Math.floor(seconds / 60);
                    if (interval >= 1) {
                        intervalType = "minute";
                    } else {
                        interval = seconds;
                        intervalType = "second";
                    }
                }
            }
        }
    }

    if (interval > 1 || interval === 0) {
        intervalType += 's';
    }

    return interval + ' ' + intervalType;
};

jsFiddle: http://jsfiddle.net/5qpxrta9/2/

The error that I'm getting:

Uncaught ReferenceError: relativeTime is not defined

user1012181
  • 8,648
  • 10
  • 64
  • 106
  • Can i ask why are you not using moment.js? – dcohenb Sep 17 '15 at 17:53
  • 1
    You say it's not working.. what do you mean by that? Is there an error? – Heretic Monkey Sep 17 '15 at 17:55
  • Use the developer tools in your browser. `Uncaught SyntaxError: missing ) after argument list` – Quentin Sep 17 '15 at 17:56
  • @Quentin: Updated this: http://jsfiddle.net/5qpxrta9/1/ was missing a quote. Still not working. – user1012181 Sep 17 '15 at 17:58
  • Continue using the developer tools. You get a new error that tells you that the function you are trying to eval is not a global. And if you fix that then you get another new error which tells you that you haven't loaded jQuery. – Quentin Sep 17 '15 at 17:59
  • @Quentin: How do I make the function as Global? (Sorry If I sounds stupid) – user1012181 Sep 17 '15 at 18:01
  • As a general rule, your code will be **much** easier to debug if you don't have 5 nested `if` statements. I'd highly recommend reading the book "Clean Code" by Robert C. Martin. If you interviewed at my company with code like this, I'd make sure you didn't get hired. Hard as that is to hear (and say), I really hope you'll take my advice on this. Good luck. – Travis Sep 17 '15 at 18:05
  • @user1012181 — You tell JS Fiddle not to wrap it on an onload handler. Better yet, you stop it needing to be a global and pass `setInterval` a function instead of a string. – Quentin Sep 17 '15 at 18:07
  • @Travis: Thanks for the Advice, but this code has to have multiple if-else to cover all possible cases. You might wanna check here: http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site – user1012181 Sep 17 '15 at 18:07
  • Still don't need 5 nested `if`s. http://jsfiddle.net/9yh9c7xh/2/ – Travis Sep 17 '15 at 18:54
  • @Travis : Agree. Sorry for the above comment. Much appreciated. – user1012181 Sep 17 '15 at 19:07

2 Answers2

1

You did not define jQuery on the fiddle, your function usage was before your function declaration, and your Date declaration was using a wrong format.

function relativeTime() {
    $('#time').text(timeSince('2015-09-17T14:59:10'));
}

setInterval(relativeTime, 1000);
Marcelo
  • 4,580
  • 7
  • 29
  • 46
0

Check out the answer from @Travis also for much better code

http://jsfiddle.net/9yh9c7xh/2/

user1012181
  • 8,648
  • 10
  • 64
  • 106