웹 개발 코드 : 간단한 타이밍, 다른 간단한 시간, 무한 루프 시간 이벤트, 시간 제한 이벤트는, 객체 인스턴스를 만들 개체에 대한 템플릿을 만들려면 시계를 만들어 사용

1 단순 타이밍

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
	function myFunction(){
		setTimeout(function(){alert("Hello")},3000);
	}
	
	</script>
</head>
<body>
<button  onclick="myFunction()">点我</button>
</body>
</html> 

(2) 또 간단한 타이밍

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
	function myFunction(){
		var x=document.getElementById('txt');
		var t1=setTimeout(function(){x.value="2秒"},2000);
		var t2=setTimeout(function(){x.value="4秒"},4000);
		var t3=setTimeout(function (){x.value="6秒"},6000);
	}
	</script>
</head>
<body>
<form>
<input type="button" value="显示文本时间!" onclick="myFunction()"/>
<input  type="text" id="txt"/>
</form>
<p>点击上面的按钮,输出的文本将告诉你2秒,4秒,6秒已经过去了。</p>
</body>
</html> 

3, 시간 제한 이벤트의 끝없는주기

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
	var c=0;
	var t;
	var timer_is_on=0;
	function timedCount(){
		document.getElementById('txt').value=c;
		c=c+1;
		t=setTimeout("timedCount()",1000);
	}
	function doTimer(){
		if(!timer_is_on)
		{
			timer_is_on=1;
			timedCount();
		}
	}
	</script>
</head>
<body>
<form>
<input type="button" value="开始计数!" onclick="doTimer()"/>
<input  type="text" id="txt"/>
</form>
<p>点击上面的按钮,输入框将从0开始计数。</p>
</body>
</html> 

4 시간 제한 이벤트에서 정지 버튼을 사용하여 무한 루프

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
	var c=0;
	var t;
	var timer_is_on=0;
	function timedCount(){
		document.getElementById('txt').value=c;
		c=c+1;
		t=setTimeout("timedCount()",1000);
	}
	function doTimer(){
		if(!timer_is_on)
		{
			timer_is_on=1;
			timedCount();
		}
	}
	function stopCount(){
		clearTimeout(t);
		timer_is_on=0;
	}
	</script>
</head>
<body>
<form>
<input type="button" value="开始计数!" onclick="doTimer()"/>
<input  type="text" id="txt"/>
<input type="button" value="停止计数!" onclick="stopCount()"/>
</form>
<p>点击上面的按钮,输入框将从0开始计数。</p>
</body>
</html> 

시간 제한 이벤트를 이용하여 5, 시계했다

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
	var c=0;
	var t;
	var timer_is_on=0;
	function startTime(){
		var today=new Date();
		var h=today.getHours();
		var m=today.getMinutes();
		var s=today.getSeconds();
		m=checkTime(m);
		s=checkTime(s);
		document.getElementById('txt').innerHTML=h+":"+m+":"+s;
		t=setTimeout(function(){startTime()},500);
	}
	function checkTime(i){
		if (i<10){
			i="0"+1;
		}
		return i;
	}
		
	</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html> 

6 개체의 인스턴스를 작성

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
person={firstname:"John",lastname:"DOE",age:50,eyecolor:"blue"}
document.write(person.firstname+" is "+person.age+" years old .")
</script>
</head>
<body></body>

</html> 

7 개체에 대한 템플릿을 만들

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>now test</title> 
<script>
	function person(firstname,lastname,age,eyecolor){
		this.firstname=firstname;
		this.lastname=lastname;
		this.age=age;
		this.eyecolor=eyecolor;
	}
myFather=new person("John","DOE",50,"blue");
document.write(myFather.firstname+" is "+myFather.age+" years old .")
</script>
</head>
<body></body>

</html> 

추천

출처blog.csdn.net/weixin_43731793/article/details/94297188