欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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>

显示当前时间

Document 当前时间:
<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>
相关标签: js时间