定时器、Date
来源:互联网 发布:送餐员下载什么软件 编辑:程序博客网 时间:2024/06/06 01:12
开启定时器
setInterval()两种写法
<script type="text/javascript">function show(){alert('a');}setInterval(show,1000);</script>
常用
<script type="text/javascript">setInterval(function(){alert('a');},1000);</script>
setTimeout()延时,只执行一次
<script type="text/javascript">function show(){alert('a');}setTimeout(show,1000);</script>
关闭定时器
<script type="text/javascript">window.onload = function() {var oBtn1 = document.getElementById('btn1');var oBtn2 = document.getElementById('btn2');var timer = null;oBtn1.onclick = function() {timer = setInterval(function() {alert('a');}, 1000)}oBtn2.onclick = function() {clearInterval(timer);}}</script>
用于处理日期和时间的Date对象
<script type="text/javascript">var myDate = new Date();console.log(myDate.getMonth() + 1); //必须加1,从0~11console.log(myDate.getFullYear()); //第几年console.log(myDate.getDay()); //星期几console.log(myDate.getDate()); //一个月的第几号console.log(myDate.getHours());//时console.log(myDate.getMinutes());//分console.log(myDate.getSeconds());//秒</script>
0 0