我有一個 Javascript 函式,它顯示一個時間計數器,如下所示:0:0 and 0:1。但我實際上希望它像這樣顯示:00:00 and 00:01。
這是代碼:
var interval = null;
var time = 0
function increaseTimer() {
// increase and print timer
time
console.log(new Date() ' ' 'timer: ' parseInt(time / 60, 10) ':' parseInt(time % 60, 10))
// condition of a process
if (time % 5 == 0) {
// some process
}
// stop simulation after 30s
if (time == 30) {
clearInterval(interval)
}
}
我用區間變數呼叫這個函式:interval = setInterval(increaseTimer, 1000)
我應該在我的代碼中進行哪些更改以使其正常作業?提前致謝。
uj5u.com熱心網友回復:
var interval = null;
var time = 0
function increaseTimer() {
// increase and print timer
time
let prefix = parseInt(time / 60, 10)
let postfix = parseInt(time % 60, 10)
prefix = prefix <= 9 ? "0" prefix.toString() : prefix.toString();
postfix = postfix <= 9 ? "0" postfix.toString() : postfix.toString();
console.log(new Date() ' ' 'timer: ' prefix ':' postfix )
// condition of a process
if (time % 5 == 0) {
// some process
}
// stop simulation after 30s
if (time == 30) {
clearInterval(interval)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442611.html
標籤:javascript 节点.js 反应
上一篇:如何修復:輸入'{源:字串;}'不可分配給型別'string'
下一篇:如何在API中插入影像?
