0

I am having a problem with an HTML5 form field, I need the field (time) to update each second to the current time, the update code is called every second via a setInterval, the HTML5 form element :

<form name="process">
    <input name="p_time" type="time" value="<?php echo date("H:i:s");?>" />
    //...
</form>

when I try to update : document.forms.process.p_time.value = current.time.slice(17,25); the display remains at the setting made by PHP, the type of string data is the same format of 00:00:00 but it refuses to budge from that original time. the variable current.time is a string of the UTC format corresponding to : ddd, dd mmm, yyyy hh:mm:ss +00:00 GMT variety.

The javascript is a simple routine, it updates a

element in the full version

current = {
    now:function(){ return new Date(); },
    time:0,
    tick:function(){
        current.time = current.now().toUTCString();
        document.forms.process.p_time.value = current.time.slice(17,25);
        current.clock();
    },
    clock:function(){
        current.target.innerHTML = current.time.slice(0,25);
    },
    auto:setInterval(current.tick,1000)
}

What I don't want to have to do is to have the page refreshed by a page refresh.

2
  • What browser are you using? The time input type is not supported in FireFox, IE and Safari. Commented Jan 11, 2015 at 21:42
  • Using Chrome Version 39.0.2171.95 m Commented Jan 11, 2015 at 23:08

1 Answer 1

2

This should work!: http://jsfiddle.net/d9vg5yhL/1/

<form name="process">
    <input name="p_time" id='time' type="time" value="" />
</form>

Javascript:

function startTime() {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    h = checkTime(h); 
    m = checkTime(m);
    s= checkTime(s);
    document.getElementById('time').value = h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},1000);
}

function checkTime(i) {
    if (i<10) {i = "0" + i} // add zero in front of numbers < 10
    return i;
}

startTime();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.