今天看了Javascript视频教程的一集,主要讲述了日期对象Date.
像往常一样,总结一下这集的小知识点吧。
这次我并不想利用1,2,3。。。归结小知识点。而是利用示例来说明。感觉这集视频教程这样来总结比较直观。
示例1:显示时间
像往常一样,总结一下这集的小知识点吧。
这次我并不想利用1,2,3。。。归结小知识点。而是利用示例来说明。感觉这集视频教程这样来总结比较直观。
示例1:显示时间
主要知识点是:
clearTimeout()方法。清除、停止时间的跳转。
setTimeout()方法。根据设定的时间跳转。
效果图:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Date对象示例:显示时间</title>
<style type="text/css">
input{
font-size:50px;
color:#cc0000;
border-style::none
}
</style>
<script type="text/javascript" >
function showNowTime()
{
var mytime=new Date();
var md=(mytime.getYear()+1900)+"-";
if(mytime.getMonth()<9)
{
md+="0";
}
md+=(mytime.getMonth()+1)+"-";
if(mytime.getDate()<=9)
{
md+="0";
}
md+=(mytime.getDate())+" ";
if(mytime.getHours()<=9)
{
md+="0"
}
md+=(mytime.getHours())+":"
if(mytime.getMinutes()<=9)
{
md+="0";
}
md+=(mytime.getMinutes()+":");
if(mytime.getSeconds()<=9)
{
md+="0";
}
md+=mytime.getSeconds();
document.getElementById("time").value=md;
setTimeout("showNowTime()",1000);
}
var settime;
var i=0;
function ShowTime()
{
i++;
alert(i);
settime=setTimeout("ShowTime()" ,2000);
}
function stopShowTime()
{
clearTimeout(settime);
}
</script>
</head >
<body οnlοad="showNowTime()">
<p align="center">
<input id="time" />
<input type="button" οnclick="ShowTime()" value="两秒弹出一次"/>
<input type="button" οnclick="stopShowTime()" value="停止弹出"/>
</p>
</body>
</html>
示例2:提示在某个时间后自动跳转到某一地址
效果图:
代码如下:
View Code
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< meta name ="keywords" content ="youlianjinda" />
< meta name ="description" content ="youlianjinda" />
< meta http-equiv ="refresh" content ="10,url=" http://www.baidu.com""
<title > 跳转 </ title >
< style type ="text/css" >
input {
font-size : 20px ;
color : #33CCFF ;
border-style : none ;
}
</ style >
</ head >
< body onload ="tiaozhuan()" >
< P align ="center" >
该网站已经停用,如果需要访问原网站内容,请访问新的地址 < a href ="http://www.baidu.com" target ="_blank" > http://www.baidu.com </ a >
点击后没有跳转,将在 < input id ="tiaozhuan" value ="10" size ="3" readonly /> 秒后自动跳转。
</ P >
< script type ="text/javascript" >
function tiaozhuan()
{
var settime;
var old = document.getElementById( " tiaozhuan " ).value;
document.getElementById( " tiaozhuan " ).value = parseInt(old) - 1 ;
if (parseInt(old) <= 1 )
{
window.location = " http://www.baidu.com " ;
clearTimeout(settime);
}
settime = setTimeout( " tiaozhuan() " , 1000 );
}
</ script >
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< meta name ="keywords" content ="youlianjinda" />
< meta name ="description" content ="youlianjinda" />
< meta http-equiv ="refresh" content ="10,url=" http://www.baidu.com""
<title > 跳转 </ title >
< style type ="text/css" >
input {
font-size : 20px ;
color : #33CCFF ;
border-style : none ;
}
</ style >
</ head >
< body onload ="tiaozhuan()" >
< P align ="center" >
该网站已经停用,如果需要访问原网站内容,请访问新的地址 < a href ="http://www.baidu.com" target ="_blank" > http://www.baidu.com </ a >
点击后没有跳转,将在 < input id ="tiaozhuan" value ="10" size ="3" readonly /> 秒后自动跳转。
</ P >
< script type ="text/javascript" >
function tiaozhuan()
{
var settime;
var old = document.getElementById( " tiaozhuan " ).value;
document.getElementById( " tiaozhuan " ).value = parseInt(old) - 1 ;
if (parseInt(old) <= 1 )
{
window.location = " http://www.baidu.com " ;
clearTimeout(settime);
}
settime = setTimeout( " tiaozhuan() " , 1000 );
}
</ script >
</ body >
</ html >