我需要在document.write中返回showtime函数来显示时间 请帮助我
链接:JS Bin
var timerID = null;
var timerRunning = false;
function showtime()
{
var now = new Date();
var hours = now.getHours();
var tm = " AM";
if (hours == 12) { tm = " PM"; }
if (hours < 12) { tm = " AM"; }
if (hours > 12) { hours = hours - 12; tm = " PM"; }
if (hours == 0) { tm = " AM"; hours = 12; }
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = "" + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
LocalTimer.innerHTML = timeValue + tm;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
您是否需要此问题的更多详细信息?
答案 0 :(得分:1)
我尝试修复代码问题并对代码进行了一些更改。我希望这符合您的要求。
var timerID = null;
var timerRunning = false;
var LocalTimer = document.getElementById("localTimer");
function showtime()
{
var now = new Date();
var hours = now.getHours();
var tm = " AM";
if (hours == 12) { tm = " PM"; }
if (hours < 12) { tm = " AM"; }
if (hours > 12) { hours = hours - 12; tm = " PM"; }
if (hours === 0) { tm = " AM"; hours = 12; }
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = "" + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
LocalTimer.innerHTML = timeValue + tm;
timerID = setTimeout(function(){showtime();},1000);
timerRunning = true;
}
showtime();
请按此link进行更新演示。