0

On my app, I have a command:

@uptime = 'uprecords -s | awk '{ print $5 }'

that returns the current uptime from computer (using uptimed).
it returns something like 01:00:00 (hours, minutes, seconds) and I want to count up this time.
How do I do that? I tried some count up jquery plugins but none of them worked like I want How do you guys count up?
Thanks in advance

edit
I think I wasn't clear enough
What I want is to catch this uptime from my server (already done it), and via javascript, make it dinamically, counting up this current uptime, so if the user got away from keyboard, by example, the uptime still increases

yesfabime
  • 814
  • 1
  • 12
  • 27
Luiz E.
  • 6,769
  • 10
  • 58
  • 98
  • 2
    "Count up" as in "parse to seconds"? "Add together"? Something else? – Ry- Jun 21 '12 at 18:28
  • If you want to convert a time to seconds, [this question](http://stackoverflow.com/questions/7301449/jquery-convertion-from-string-to-time) is an exact duplicate. However, I'm not totally sure if converting to seconds is what you want; please clarify your question. – apsillers Jun 21 '12 at 18:34
  • 2
    You want to grab the _computer's_ uptime using JavaScript? – Salman A Jun 21 '12 at 18:36
  • @minitech I didn't got...@apsillers what I want is something like this: http://www.bloke.com/javascript/Countup/ – Luiz E. Jun 21 '12 at 18:37

2 Answers2

2

You can use setInterval:

var seconds = 2642; // uptime in seconds
var timer = setInterval(
    function() {
        seconds++;
    }, 1000
);

Also, see this reference on JS time functions.

avramov
  • 2,119
  • 2
  • 18
  • 41
  • I see; it seems `setInterval` can behave strangely when the window loses focus. If that's your problem, I guess you'll need to re-fetch the time from your server using Ajax every time the window regains focus, then. For simple Ajax calls, see http://microjs.com/#ajax. If you need to *display* the time, you'll need DOM manipulation - see http://microjs.com/#dom. – avramov Jun 21 '12 at 18:47
  • nope, that's not my problem...my problem is that I want to make the uptime dinamical...I don't think that ajax calls will be a good think – Luiz E. Jun 21 '12 at 18:50
  • What do you mean dynamical? Incrementing it with setInterval *is* dynamical - a second is always a second, more or less. One second your uptime will be 8000 seconds, the next it will be 8001, 8002, etc. If you need to display it on a page so that it updates each second, see my remark about DOM manipulation - i.e., editing the contents of the elements on your page. – avramov Jun 21 '12 at 18:52
  • See this reference on [JS date and time functions](http://www.w3schools.com/jsref/jsref_obj_date.asp). – avramov Jun 22 '12 at 10:51
1

I've done something (ugly) like this:

  function atualizarTimer() {
    //span.uptimed is a string like 01:23:45
    var time = $('span.uptimed').text();
    var d = new Date();
    times = time.split(':');
    d.setHours(times[0]);
    d.setMinutes(times[1]);
    d.setSeconds(times[2]);
    d.setSeconds(d.getSeconds()+1);

    document.getElementById("uptimed").innerHTML = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
  }
Luiz E.
  • 6,769
  • 10
  • 58
  • 98