然後作為一個計時器可是總是顯示undefined
小米手環文件
https://cdn.discordapp.com/attachments/1132882463522312204/1132987644255670322/helloworld_pro.bin
var time_val;
var go_timer;
var stop_time = 0;
function add_zero(num, digit) {
let numString = String(num)
while (numString.length < digit) {
numString = "0" + numString
}
return numString
}
function timers() {
if (true) {
let temp_timer = new Date().getTime() - go_timer;
let day = add_zero(Math.floor(temp_timer / (24 * 60 * 60 * 1000)), 2);
let hours = add_zero(Math.floor((temp_timer % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000)), 2);
let minutes = add_zero(Math.floor((temp_timer % (60 * 60 * 1000)) / (60 * 1000)), 2);
let seconds = add_zero(Math.floor((temp_timer % (60 * 1000)) / 1000), 2);
let milliseconds = add_zero(temp_timer % 1000, 3);
return [day, hours, minutes, seconds, milliseconds];
}
}
go_timer = new Date().getTime();
time_val = setInterval(() => {
var time = timers();
console.log(time[0] + "天\n" + time[1] + "時\n" + time[2] + "分\n" + time[3] + "秒\n" + time[4] + "毫秒");
}, 1);
/*
undefined天
undefined時
undefined分
undefined秒
undefined毫秒
*/
var time_val;
var go_timer;
var stop_time = 0;
function add_zero(num, digit) {
let numString = String(num)
while (numString.length < digit) {
numString = "0" + numString
}
return numString
}
go_timer = new Date();
time_val = setInterval(() => {
let temp_timer = new Date().getTime() - go_timer.getTime();
let day = add_zero(Math.floor(temp_timer / 1000 / 60 / 60 / 24), 2);
let hours = add_zero(Math.floor(temp_timer / 1000 / 60 / 60) % 24, 2);
let minutes = add_zero(Math.floor(temp_timer / 1000 / 60) % 60, 2);
let seconds = add_zero(Math.floor(temp_timer / 1000 % 60), 2);
let milliseconds = add_zero(temp_timer % 1000, 3);
var timers = [day, hours, minutes, seconds, milliseconds];
console.log(timers[0] + "天\n" + timers[1] + "時\n" + timers[2] + "分\n" + timers[3] + "秒\n" + timers[4] + "毫秒");
}, 1)
程式碼沒問題呀
我剛貼到 jsfiddle 跑很正常呀
https://jsfiddle.net/poopoo888888/aq25x3t6/