我試圖根據一天中的時間更新html元素內的內容,我正在做類似於以下內容的內容:Updating div content according to time但我希望內容更新沒有頁面刷新。根據一天中的時間在html元素中實時更新內容
代碼:
function schoolPeriod() {
var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
// prepend minutes with 0 if < 10
if (m < 10) {
m = "0" + m;
}
h = h.toString();
m = m.toString();
var t = h + m;
var periods = [
"school assembly",
"first period",
"second period",
];
var currentPeriod = document.querySelector('.current-period');
if (t >= 600) {
currentPeriod.innerHTML = "new school day";
}
else if (t >= 900) {
currentPeriod.innerHTML = periods[0];
}
else if (t >= 940) {
currentPeriod.innerHTML = periods[1];
}
else if (t >= 1020) {
currentPeriod.innerHTML = periods[2];
}
setTimeout(schoolPeriod, 1000);
}
schoolPeriod();
codepen:http://codepen.io/carpenumidium/pen/grRYoN?editors=1011
我在做什麼錯?感謝幫助!
@nnnnnn那完全工作!謝謝!如果您將它作爲一個發佈,我會接受您的答案! :) – carpenumidium