<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<script>
function check(data) {
if (data < 10) {
return `0${data}`
} else {
return data;
}
}
function dateInit() {
var dateNow = new Date();
function getYear() {
var yearNow = dateNow.getFullYear();
yearNow = check(yearNow);
return yearNow;
}
function getMonth() {
var monthNow = dateNow.getMonth();
monthNow = check(monthNow);
return monthNow;
}
function getDate() {
var dayNow = dateNow.getDate();
dayNow = check(dayNow);
return dayNow;
}
function getHour() {
var hourNow = dateNow.getHours();
hourNow = check(hourNow);
return hourNow;
}
function getMinus() {
var minusNow = dateNow.getMinutes();
minusNow = check(minusNow);
return minusNow;
}
function getSeconds() {
var SecondsNow = dateNow.getSeconds();
SecondsNow = check(SecondsNow);
return SecondsNow;
}
function getDay() {
var dayArr = ["", "", "", "", "", "", ""];
var dayNow = dateNow.getDay();
return dayArr[dayNow];
}
}
function writeDate() {
dateInit();
var spans = document.querySelectorAll("span");
spans[0].innerHTML = getYear() + " ";
spans[1].innerHTML = getMonth() + " ";
spans[2].innerHTML = getDate() + " ";
spans[3].innerHTML = getDay();
spans[4].innerHTML = getHour() + "";
spans[5].innerHTML = getMinus() + "";
spans[6].innerHTML = getSeconds() + "";
var timer = setInterval(function () {
writeDate();
}, 1000)
}
writeDate()
</script>
</body>
</html>
this is an automatically updated clock I wrote to allow browsers to report getYear is not defined . My understanding is that I have defined the getYear function globally and should be able to access it in writeDate (). I don"t know where there is an error in understanding, and how to change the code, which makes me a little confused now. Please point out, thank you very much.