hello guys! am trying to put time on my website, and i want the seconds to be changing each time, but untill you refresh page!

i've tred using the setTimeout() method, but to no avail! help please.

this is my code:

// Page Times JavaScript Document

window.onload = initDate;
var now = new Date();


function initDate(){

var dayName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");



var dtString = dayName[now.getDay()] +" "+ now.getDate() +" "+ monName[now.getMonth()] +" "+ now.getFullYear()+"<br>"+ showTime();


document.getElementById("tmDate").innerHTML = dtString;
    }





function showTheHours(theHour){
    if(theHour==0){
        return 12;
        }

    else if(theHour<13){
        return theHour;
        }

    else return theHour-12;
    }   


function showZeroField(inValue){
    if(inValue>9){

        return ":"+inValue;
        }
    else return ":0"+inValue;

    }


function showAmPm(){
    if(now.getHours()<12){
        return " am";
        }

    else return " pm";
    }   

function showTime(){
var theTime = showTheHours(now.getHours())+ showZeroField(now.getMinutes())+showZeroField(now.getSeconds())+showAmPm();
    return theTime; 
    setTimeout("showTime()", 1000);
    }
Member Avatar for stbuchok

Wouldn't it be better to use setInterval than setTimeout? Also, in the showTime function, you are retuning before you even get to the setTimeout, so how are you expecting to have it called if it never reaches it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.