js时间案例(里面涉及的方法都在js相关操作中)
程序员文章站
2024-01-28 09:21:40
...
会员到期还有几天
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="timeTurn.js"></script>
</head>
<body>
会员试用期为7天,距离到期还有:<span></span>
<script>
//此方法有待改进,不能记录开通时间
var start = new Date();
const end = start.setDate(start.getDate()+7);
console.log(end);
var span = document.getElementsByTagName('span')[0];
setInterval(function(){
var d = new Date();
var sec = daojishi(d,end);
var t = turnSeconds(sec);
span.innerText = t;
},1000);
</script>
</body>
</html>
图片轮播
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="timeTurn.js"></script>
</head>
<body>
<!-- 开启定时器,每隔固定一段时间修改图片src,将所有图片存在一个数组中,只需改变索引 -->
<img src="" alt="">
<script>
var img = document.getElementsByTagName('img')[0];
var arr = ['img1','img2','img3'];
var index = 0;
turnIndex(arr,index);
</script>
</body>
</html>
显示当前时间
<script>
var span = document.getElementsByTagName('span')[0];
setInterval(function () {
var d = new Date();
span.innerText = toString(d);
}, 1000);
</script>
倒计时
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="timeTurn.js"></script>
</head>
<body>
距离2020年还有:<span></span>
<script>
var end = new Date("2020-1-1");
var start = new Date();
var span = document.getElementsByTagName('span')[0];
span.innerHTML = turnSeconds(daojishi(start,end));
setInterval(function(){
var start = new Date();
var t = turnSeconds(daojishi(start,end));
span.innerHTML = t;
},1000);
</script>
</body>
</html>
上一篇: JavaScript系列之基础篇(1)
下一篇: 回调函数 同步回调 异步回调