Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to periodically update a date/time value on a page using jQuery?

In this example, I have an input text field that contains a datetime value set with jQuery.

http://jsfiddle.net/D4Hu9/

HTML:

<input id="foo" name="foo" type="text" value="" size="55" />

JS:

jQuery(document).ready(function()
{
    var now = new Date();
     jQuery('#foo').val(now.toString());        
});

How can I update this value every 5 seconds from jQuery?

like image 928
Kevin Avatar asked Jan 19 '23 16:01

Kevin


1 Answers

function updateTime() {
    var now = new Date();
    jQuery('#foo').val(now.toString());        
}

setInterval(updateTime, 5000); // 5 * 1000 miliseconds

Here's the fiddle: http://jsfiddle.net/28Tgz/1/

like image 135
Magnar Avatar answered Jan 29 '23 16:01

Magnar