2

I'm going to sound like such a noob, but basically all I would like to achieve is a script which would automatically refresh, as to speak, the time something was posted. For example;

<div id="update" data-time="2012-08-16 20:48:11">Posted 12 secs ago</div>

*then, 12 seconds later*

<div id="update" data-time="2012-08-16 20:48:11">Posted 24 secs ago</div> It doesn't have to be formatted like that, it was just an example I thought I could use.

I would prefer not to use an external plugin for this task also I wouldn't just like it in seconds, I would prefer it to be like "x secs" then when it had reached 60 seconds turn into "x mins" etc.

You'd be helping me out a lot if I could have a response, whether its just a link to point me in the right direction or a section of code I could use. Thank you :)

  • http://stackoverflow.com/questions/1245617/want-a-javascript-function-to-run-every-minute-but-max-3-times – kavun Oct 10 '12 at 17:27

2 Answers2

1

Please look at JavaScript's raw setTimeout() and setInterval() http://www.w3schools.com/js/js_timing.asp

ilan berci
  • 3,883
  • 1
  • 16
  • 21
  • Upvote for good approach, but setInterval() is best avoided. http://stackoverflow.com/questions/7142192/is-setinterval-and-settimeout-bad-things-to-do-in-modern-jquery-animations – jmh Oct 10 '12 at 17:41
0

You could use something like this:

var div = $('#update'),
content;

var myUpdater = setInterval(function () {
    content = div.html();
    div.html(content.replace(/(\d+)/g, function (captureGroup1) {
        var time = parseInt(captureGroup1, 10) + 12;
        return time;
    }));
}, 12*1000);​

The example above shows you how you can change number of seconds with setInterval function. You can improve it by make some conditions like whether number of seconds more than 60 or not and change your output accordingly.

happyCoda
  • 418
  • 2
  • 2