JS倒计时效果

时间:2022-02-23 22:05:19


http://www.imooc.com/learn/59

使用JavaScript的技术,输出当前系统时间到页面指定位置。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>当前系统时间</title>
<link rel="stylesheet" href="style.css" />
<script language="javascript" type="text/javascript">

window.onload = function(){
showTime();
}
function checkTime(i){ //补位处理,一位时,前面补0

return i<10? ' 0'+i:' '+i;


}
function showTime(){
var now=new Date();
var year= now.getFullYear() ;
var month= now.getMonth()+1 ;
var day= now.getDate() ;
var h= now.getHours() ;
var m= now.getMinutes() ;
var s= now.getSeconds() ;
m=checkTime(m)
s=checkTime(s)

var weekday='星期'+'日一二三四五六'.charAt(now.getDay());

document.getElementById("show").innerHTML=""+year+"年"+month+"月"+day+"日 "+ weekday +h+":"+m+":"+s;
setTimeout('showTime()',500)
}

</script>
</head>
<body>
<div class="content1">
<div id="show">显示时间的位置</div>
</div>
</body>
</html>

使用JavaScript技术,实现使用时间差来转换倒计时效果。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>倒计时时间</title>
<link rel="stylesheet" href="style.css" />
<script language="javascript" type="text/javascript">
window.onload = function(){
  var timedate= new Date("2017,3,30"); //自定义结束时间
  var now = new Date() ; //获取当前时间
  var date = timedate.getTime() - now.getTime(); //得出的为毫秒
  var time = Math.ceil(date/(1000 * 60 * 60 * 24)) ;
  //1000 * 60 * 60 * 24一天的秒数
if(time > 0 ){
document.getElementById('timeShow').innerHTML = time;
}
}
</script>
</head>
<body>
<div class="content2">
<div class="txtshow">距离设置时间还有<span id="timeShow"></span></div>
</div>
</body>
</html>

使用JavaScript技术,实现团购、限时抢等效果

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>团购——限时抢</title>
<link rel="stylesheet" href="style.css" />
<script>
function FreshTime()
{

var end=new Date("2017/5/15,12:20:12");//结束时间
var now = new Date();//当前时间
var lefttime = parseInt((end.getTime()-now.getTime()));
var d = parseInt(lefttime/(24*60*60*1000));
var h = parseInt((lefttime/(60*60*60*1000))%24);
var m = parseInt(lefttime/(60*1000)%60);
var s = parseInt(lefttime/1000%60);

document.getElementById("LeftTime").innerHTML=d+"天"+h+"小时"+m+"分"+s+"秒";
if(lefttime<=0){
document.getElementById("LeftTime").innerHTML="团购已结束";
clearInterval(sh);
}
}
var sh;
sh=setInterval(FreshTime,500) ;
</script>
</head>

<body>
<div class="content3">
<div class="time">还剩 <span id="LeftTime"></span></div>
</div>

</body>
</html>