1

私は奇妙な問題を抱えています..私は秒カウンターで時間を表示する機能を持っていますが、秒のカウントが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);
4

2 に答える 2

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

ここからこのコードを取得しました

于 2013-01-21T10:01:40.123 に答える
0

ループが 60 カウントを通過するか、または 59 で終了するかどうかを確認してください。

于 2013-01-21T09:31:22.630 に答える