关于JAVASCRIPT中setInterval函数的一些用法

就setInterval()方法写了个小DEMO,一个时钟显示,结合了Date类型的API。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
<script type="text/javascript">

function testSetInterval(){
  
    setInterval(clock,1000);
}
function clock(){
  var time = new Date();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  var year = time.getYear();
  var month = time.getMonth();
  var day = time.getDay();
  var yearDiv = document.getElementById('year');
  yearDiv.innerHTML = year;
  var monthDiv = document.getElementById('month');
  monthDiv.innerHTML = month;
  var dayDiv = document.getElementById('day');
  dayDiv.innerHTML = day;
  var hourDiv = document.getElementById('hour');
  hourDiv.innerHTML = hour;
  var minuteDiv = document.getElementById('minute');
  minuteDiv.innerHTML = minute;
  var secondDiv = document.getElementById('second');
  secondDiv.innerHTML = second;
}

</script>
</head>

<body οnlοad="testSetInterval()">
   <span id="year"></span>年<span id="month"></span>月<span id="day"></span>日&nbsp;&nbsp;&nbsp;&nbsp;<span id="hour"></span>:<span id="minute"></span>:<span id="second"></span>
</body>
</html>

 

 

 

很麻烦,要把年月日时分秒全球一遍。汗呀,忽然发现Date类有一个tolocaleString()方法,马上应用,简单利落呀!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
<script type="text/javascript">
function testSetInterval(){ 
    setInterval(clock,1000);
}
function clock(){
   var date = document.getElementById('date');       
   date.innerHTML = new Date().toLocaleString();
}

</script>
</head>

<body οnlοad="testSetInterval()">
<div id="date"></div>
</body>
</html>

 

利用setInterval(clock,1000);每隔1秒钟调用clock方法更新页面组件值。达到时钟的效果。

 

实际上setInterval方法还有许多用法,举例说明

有以下两种重载形式

setIntervar(methodname,interval)

setIntervar("methodname(arg1,arg2)",interval)

setIntervar("objname.methodname(arg1,arg2)",interval);

看下面例子:

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
<script type="text/javascript">
function setObjInterval(){
   this.warn = function(warninginfo,times){
   alert('warninginfo is ' + warninginfo + 'and times is '+times);
   }
}
var objInterval = new setObjInterval();

//测试调用对象的方法
function testSetObjInterval(){  
    setInterval("objInterval.warn('time is over', 33)", 2000);
}
function testSetInterval(){ 
    setInterval(clock,1000);
}
function clock(){
   var date = document.getElementById('date');       
   date.innerHTML = new Date().toLocaleString();
}
function setIntervalManyArg(){
   setInterval("showInfo('shl',22)", 2000);
}
function showInfo(name){
   alert('name is ' + name + ' and age is ' + age);
}
</script>
</head>

<body οnlοad="testSetInterval()">
<div id="date"></div>
   <input type='button' value="click me" οnclick='testSetObjInterval()'/>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
动态时钟 body,div,p{ font-family: 'Microsoft Yahei' ;font-size: 14px;} .box{ width: 400px; height: 400px; border:10px solid #8bf2f1;margin:100px auto; border-radius: 50%; box-shadow: 0px 0px 20px 3px #444 inset; position: relative;} /*原点*/ .origin{ width: 20px; height: 20px; border-radius: 50%; background: #ff0000; top:190px; left: 190px; position: absolute;} /* 指针 */ .clock_line{ position: absolute;position:absolute;z-index:20;} .hour_line{width:100px;height:4px;top:198px;left:200px;background-color:#000;border-radius:2px; transform-origin:0 50%;box-shadow:1px -3px 8px 3px #aaa;} .minute_line{width:130px;height:2px;top:199px;left:190px;background-color:#000; transform-origin:7.692% 50%;box-shadow:1px -3px 8px 1px #aaa;} .second_line{width:170px;height:1px;top:199.5px;left:180px;background-color:#f60; transform-origin:11.765% 50%;box-shadow:1px -3px 7px 1px #bbb;} .dot_box{width: inherit; height: inherit;} /*时钟数*/ .dot{ width: 40px; height: 40px; line-height: 40px; text-align: center; font-size: 22px; position: absolute;} .clock-scale{width:195px;height:2px;transform-origin:0% 50%;z-index:7; position:absolute;top:199px;left:200px;} .scale-show{ width:12px;height:2px;background-color:#555;float:left;} .scale-hidden{width:183px;height:2px;float:left;} /*日期*/ .date_info{width:160px;height:28px; line-height:28px;text-align:center;position:absolute;top:230px;left:120px;z-index:11;color:#555; font-weight:bold;} .time_info{ width: 110px; height: 35px; line-height: 35px;text-align:center;position:absolute;top:270px;left:150px;z-index:11;color:#555; background: #253e3e; } .time{ width: 35px ;height:35px; float: left; color: #fff; font-size: 22px;} #minute_time{border-left:1px solid #fff;border-right:1px solid #fff;} <div class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道化简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值