我可以在我的时区PST(美国/洛杉矶)显示和运行当前时间,我也可以显示EST - (美国/纽约)。我的问题是,当我的当前时间显示在不同时区运行的秒数时,只显示页面加载的时间并停止运行。我在我的两个函数中使用setInterval()函数,负责显示每个时区的时间。这是我的代码:
$(document).ready(function(){
//function to display time in current time zone (PST)
function timeDisplay() {
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var meridiem = " ";
if(hours >= 12){
hours = hours - 12;
meridiem = "pm";
}
else{
meridiem = "am";
}
if(hours === 0){
hours = 12;
}
if(hours < 10){
hours = "0" + hours;
}
if(minutes < 10){
minutes = "0" + minutes;
}
if(seconds < 10){
seconds = "0" + seconds;
}
var clockDiv = document.getElementById('clock');
clockDiv.innerText = hours + ":" + minutes + ":" + seconds + " " + meridiem;
}
timeDisplay();
setInterval(timeDisplay, 1000);
//function to display time in (EST) New York
function newYorkTimeDisplay(offset) {
var currentTime = new Date();
currentTime.setHours(currentTime.getHours()+offset);
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var meridiem = " ";
if(hours >= 12){
hours = hours - 12;
meridiem = "pm";
}
else{
meridiem = "am";
}
if(hours === 0){
hours = 12;
}
if(hours < 10){
hours = "0" + hours;
}
if(minutes < 10){
minutes = "0" + minutes;
}
if(seconds < 10){
seconds = "0" + seconds;
}
var newYorkDiv = document.getElementById('newYork');
newYorkDiv.innerText = hours + ":" + minutes + ":" + seconds + " " + meridiem;
}
newYorkTimeDisplay(+3);
setInterval(newYorkTimeDisplay(+3), 1000);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div>
<div id='clock'></div>
<div id="newYork"></div>
</div>
</body>
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我想在一个页面上显示来自大约四个不同城市的运行时钟。任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
我认为你不能传递像这样的参数。最简单的选择可能是恢复参数,纽约音调总是在3小时后发布。
或者尝试......
{ ... }
或者您可以尝试使用匿名函数,例如......
setInterval("newYorkTimeDisplay(+3)", 1000);
答案 1 :(得分:0)
您需要将实际函数newYorkTimeDisplay
传递给setInterval
(而不是函数的返回值),并确保将+3
等其他参数传递给它,如下所示:< / p>
setInterval(newYorkTimeDisplay, 1000, +3);
<小时/>
$(document).ready(function(){
//function to display time in current time zone (PST)
function timeDisplay() {
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var meridiem = " ";
if(hours >= 12){
hours = hours - 12;
meridiem = "pm";
}
else{
meridiem = "am";
}
if(hours === 0){
hours = 12;
}
if(hours < 10){
hours = "0" + hours;
}
if(minutes < 10){
minutes = "0" + minutes;
}
if(seconds < 10){
seconds = "0" + seconds;
}
var clockDiv = document.getElementById('clock');
clockDiv.innerText = hours + ":" + minutes + ":" + seconds + " " + meridiem;
}
timeDisplay();
setInterval(timeDisplay, 1000);
//function to display time in (EST) New York
function newYorkTimeDisplay(offset) {
var currentTime = new Date();
currentTime.setHours(currentTime.getHours()+offset);
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var meridiem = " ";
if(hours >= 12){
hours = hours - 12;
meridiem = "pm";
}
else{
meridiem = "am";
}
if(hours === 0){
hours = 12;
}
if(hours < 10){
hours = "0" + hours;
}
if(minutes < 10){
minutes = "0" + minutes;
}
if(seconds < 10){
seconds = "0" + seconds;
}
var newYorkDiv = document.getElementById('newYork');
newYorkDiv.innerText = hours + ":" + minutes + ":" + seconds + " " + meridiem;
}
newYorkTimeDisplay(+3);
setInterval(newYorkTimeDisplay, 1000, +3);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div>
<div id='clock'></div>
<div id="newYork"></div>
</div>
</body>