JS
1.显示日期:
<script>
function displayDate(){
document.getElementById("demo").innerHTML=Date();
}
.....
<button type="button" onclick="displayDate()">显示日期</button>
2.点击一秒后出现提示框
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
setTimeout(function(){alert("Hello")},1000);
}
</script>
3.无限循环计时器,有开始/停止功能
<script>
var c=0;
var t;
var timer_is_on=0;
function timedCount(){
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout(function(){timedCount()},1000); //1000ms表示1S
}
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>
<script>
function startTime(){
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();// 在小于10的数字前加一个‘0’
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" + i; // 在小于10的数字前加一个‘0’
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
4.信息的拼接
雯雯已经50个了
<body>
<script>
function person(firstname,lastname,age,eyecolor){
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
myFather=new person("雯雯"," ",15,"blue");
document.write(myFather.firstname + "已经" + myFather.age + "个了");
</script>
</body>
//也可以这样写
<body>
<script>
person={firstname:"雯雯",lastname:" ",age:15,eyecolor:"blue"}
document.write(person.firstname + "已经" + person.age + "岁了");
</script>
</body>