【问题标题】:current time with seconds当前时间(秒)
【发布时间】:2013-01-21 09:17:05
【问题描述】:

我有一个奇怪的问题..我有一个用秒计数器显示时间的功能,但是当秒数达到 59 时,我发送了特定时间(小时和分钟)作为参数,没有增加分钟,我不确定是否同样的问题小时数为 59 分钟

function updateClock(current_hours,current_minutes) {
var currentTime = new Date ( );

var currentHours    = current_hours;
var currentMinutes  = current_minutes;
var currentSeconds  = currentTime.getSeconds ( );

// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

// Choose either "AM" or "PM" as appropriate
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

// Convert the hours component to 12-hour format if needed
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;

// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

// Big font time
$('.big_time').html("<span></span>" + currentTimeString);

// Time in red triangle in the track bar
$('.time').html(currentTimeString);
}

我在准备好的文档中的另一个文件中调用此函数.. 我将从 API 检索到的数据发送到这里(小时和分钟)

current_hours   = data.current_hours;
current_minutes = data.current_minutes;

setInterval(function(){updateClock(current_hours,current_minutes)},1000);

【问题讨论】:

  • 能否请您在调用updateClock的位置发布代码?
  • 如果你一直经过相同的小时和分钟,显然它不会改变:)
  • 请检查我对问题所做的编辑以获得更多说明可能是我遗漏了一些东西
  • @YasserMoussa 您只检索一次值,并且您从未在代码中更新它们,所以我真的不知道您为什么希望它们改变。例如,如果您不断输入1027,那么无论秒数是多少,都会始终显示时间。

标签: php javascript jquery


【解决方案1】:

改用这个

function updateClock ( )
    {
    var currentTime = new Date ( );
    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    var currentSeconds = currentTime.getSeconds ( );

    // Pad the minutes and seconds with leading zeros, if required
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

    // Choose either "AM" or "PM" as appropriate
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

    // Convert the hours component to 12-hour format if needed
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

    // Convert an hours component of "0" to "12"
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    // Compose the string for display
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
    
    
    $("#clock").html(currentTimeString);
        
 }

$(document).ready(function()
{
   setInterval('updateClock()', 1000);
});

here获得此代码

【讨论】:

  • 好的,但关键是我想读取小时和分钟作为发送给函数的参数,因为我从 API 检索小时和分钟
【解决方案2】:

例如,请检查循环是否经过 60 次计数或以 59 结束 for(i=1,i

【讨论】:

  • 发布的代码中没有一个循环,也没有提到文本中使用了一个循环,不知道为什么你会假设正在使用一个循环。这应该(充其量)作为评论发布(我知道你还不能这样做,但这并不意味着将其作为答案发布)。
  • 秒数在 59 处结束,然后回到 00,但分钟数没有增加,只是秒数一次又一次地重新计数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 2014-07-25
  • 2010-10-02
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多