当前时间与秒

时间:2013-01-21 09:17:06

标签: php javascript jquery

我有一个奇怪的问题..我有一个功能,以秒计数器显示时间,但我发送特定时间(小时和分钟)作为参数当第二次计数达到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);

2 个答案:

答案 0 :(得分:2)

改为使用

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);
});

从[{3}}

获取此代码

答案 1 :(得分:0)

请检查循环是否经过60次计数或以59结束本身为止 对于(i = 1,i <= 60:i ++),它将转到59并结束,检查开始和结束值。