Search code examples
javascriptstopwatch

Javascript Stopwatch Insert into HTML


I've been looking at this code:

http://www.jquery4u.com/jquery-date-and-time-2/online-jquery-stopwatch/

I'm trying to understand exactly how it is working because I'd like a timer on a page I'm working on.

I can't for the life of me work out where the value of the timer is added to the input "disp" and how I could put this into a regular text, not a form input.

The only thing I can see that refers to it is:

t[6]=document.getElementById('disp');

Can anyone help me understand this please,

Thanks


Solution

  • You've correctly identified that they use t[6] to refer to the HTML element. But to set it, earlier on there is:

    function disp() {
        if (t[2]) t[1]=(new Date()).valueOf();
        t[6].value=format(t[3]+t[1]-t[0]);
    }
    

    Since t[6] is the form input, they set the value of the form input to the formatted time. If you wanted to use some other element, e.g. <div id="myTime"></div>, you could say instead:

    function disp() {
        if (t[2]) t[1]=(new Date()).valueOf();
        document.getElementById("myTime").innerText = format(t[3]+t[1]-t[0]);
    }
    

    Which changes only the last line, so that you may set the text of your div.