m0_38034574
m0_38034574
2019-02-03 12:45

原生JS代码计时,为什么秒数不随时间刷新?

5
  • html5
  • ecmascript

<!DOCTYPE html>



day25-27(1)

var d = new Date(); var res = document.getElementById("result"); var t; function time() { var year = d.getFullYear(); var month = d.getMonth()+1; var day = d.getDate(); var weekday = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]; var week = d.getUTCDay()+1; var hour = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); minutes = checkTime(minutes); seconds = checkTime(seconds); res.innerHTML = year + "年" + month + "月" + day + "日"+ " " + weekday[week] + " " + hour + ":" + minutes + ":" + seconds; console.log(seconds); t = setTimeout(time, 1000); } function checkTime(i) { if (i<10) {i="0" + i} return i } time();


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答