저는 일반적으로 TypeScript와 OOP를 처음 사용합니다. 숙제로 간단한 디지털 시계를 만들어야합니다. 나는 수업을 사용해야한다. JS에서 만드는 것이 더 쉬웠으므로 잘 했으므로 제대로 작동합니다. 이제 TypeScript로 "되돌리기"위해 노력하고 있습니다. 또한 시간대를 변경하는 기능을 추가해야합니다.TypeScript에서 시계를 만드는 방법은 무엇입니까?
도와주세요!
내 작업 JS 버전은 다음과 같습니다
이var clock = document.getElementById('clock');
function Clock() {
var time = new Date();
var hours = time.getHours().toString();
var minutes = time.getMinutes().toString();
var seconds = time.getSeconds().toString();
if (hours.length < 2) {
hours = '0' + hours;
}
if (minutes.length < 2) {
minutes = '0' + minutes;
}
if (seconds.length < 2) {
seconds = '0' + seconds;
}
var clockStr = hours + ' : ' + minutes + ' : ' + seconds;
clock.textContent = clockStr;
}
Clock();
setInterval(Clock, 1000);
타이프 라이터 나의 작동하지 않는 코드는 다음과 같습니다
class Clock {
hours:number;
minutes:number;
seconds:number;
constructor(hours:number, minutes:number, seconds:number) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
print() {
if (hours.length < 2) {
hours = '0' + hours;
}
if (minutes.length < 2) {
minutes = '0' + minutes;
}
if (seconds.length < 2) {
seconds = '0' + seconds;
}
var clockStr = hours + ' : ' + minutes + ' : ' + seconds;
}
}
function timeGenerate() {
let time = new Date();
let hours = time.getHours();
let minutes = time.getMinutes();
let seconds = time.getSeconds();
}
function clockRun() {
timeGenerate();
let newClock = new Clock(hours, minutes, seconds);
newClock.print();
}
clockRun();
setInterval(clockRun, 1000);
document.getElementById('tsClock').innerHTML = newClock;
내 HTML : 우리가 갈 직전에
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typescript Clock</title>
<style>
body {
background-color: royalblue;
font-family: 'Lato';
margin-top: 300px;
text-align: center;
color: ivory;
}
#clock {
font-weight: 300;
font-size: 100px;
}
</style>
</head>
<body>
<h1 id="tsClock"></h1>
<script src="app.js"></script>
</body>
</html>
타이프 모든 유효한 JS가 유효한 수단 스크립트의 수퍼,가요 TS. 처음에는 인수가 모두 'new Clock (시, 분, 초)'로 정의되어 있지 않습니다. – Phix
나이가 아닌 일부 타이피 스크립트 마술 : https://gottz.de/uhr.htm https://gottz.de /binuhr.htm https://gottz.de/analoguhr.htm – GottZ
@GotZ 멋지다! 코드를 공유 할 수 있습니까? –