Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  4] [ 3]  / answers: 1 / hits: 57215  / 12 Years ago, wed, september 5, 2012, 12:00:00

i have some clock script. Everything is fine and it's work perfectly but... i have one problem. If at the clock is set one digit hour or minute like 1:5 clock not adding 0 digit before. This what i'v done but it does't work. Can u help me, much thx?



window.setInterval(function update_clock() {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
$.ajax({
success: function (clock) {
document.getElementById(hour).firstChild.nodeValue = currentHours;
document.getElementById(minutes).firstChild.nodeValue = currentMinutes;
if (currentMinutes.length == 1) {
currentMinutes = 0 + currentMinutes;
}
}
});
}, 999);

More From » clock

 Answers
12

currentMinutes is a number, so it does not have the length property. Also, you must check the length before set the currentMinutes to the minutes element.



Something like:



var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();

$.ajax({
success: function (clock) {
if (currentMinutes.toString().length == 1) {
currentMinutes = 0 + currentMinutes;
}

document.getElementById(hour).firstChild.nodeValue = currentHours;
document.getElementById(minutes).firstChild.nodeValue = currentMinutes;
}
});

[#83238] Monday, September 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleedayanarar

Total Points: 303
Total Questions: 90
Total Answers: 102

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
;