2013-01-21 112 views
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); 
+0

你能請張貼你在哪裏調用'updateClock'的代碼? – C5H8NNaO4

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

得到這個代碼here

+0

好偉大的,但問題是,我想讀小時和分鐘爲發送給函數的參數,因爲我檢索小時和分鐘一個API –

+0

好的。讓我檢查一下 – Roger

0

請檢查循環是否經過60計數或結束於59本身,例如 (i = 1,i < = 60:i ++)它會到達59並結束,檢查開始和結束值。

+0

發佈的代碼中沒有單個循環,也沒有提到文本中正在使用的循環,不知道爲什麼你會假設正在使用循環。這應該(最好)作爲評論發佈(我知道你還不能做,但這並不意味着將其作爲答案發布)。 –

+0

秒結束計數爲59,然後回到00,但在幾分鐘內沒有增量又幾秒鐘內重新計票,並再次 –