Use setInterval :
setInterval(clock, 1000); function clock() { var date = new Date(); var h = date.getHours(); if(h<10) h = "0"+h; var m = date.getMinutes(); if(m<10) m = "0"+m; var s = date.getSeconds(); if(s<10) s = "0"+s; document.write(h + " : " + m + " : " + s); }
Although you probably want to update the HTML element and not document.write on the page every second.
http://jsfiddle.net/bQNwJ/
source share