0

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);
    }
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by stbuchok
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.