用js实现倒计时,可以自己设定时间

时间:2022-06-23 22:08:22

这个*应该是烂大街了,但是身为新手,我还是自己写了一下。虽然成功了,但是还待提高。

js代码:

function show(){
function mess(){
var year=Number(document.getElementById("inyear").value);
var month=Number(document.getElementById("inmonth").value)-1;
var day=Number(document.getElementById("inday").value);
var hour=Number(document.getElementById("inhour").value);
var minute=Number(document.getElementById("inminute").value);
var second=Number(document.getElementById("insecond").value);
var riqi= new Date(year,month,day,hour,minute,second);
var nowdate=Date.now();
var cha=Math.round((riqi-nowdate)/1000);
var day1=parseInt(cha/(3600*24));
var left_day=cha%(3600*24);
var hour1=parseInt(left_day/3600);
var hour_left=left_day%3600;
var minute1=parseInt(hour_left/60);
var second1=hour_left%60;
document.getElementById("1").innerHTML=day1+"天";
document.getElementById("2").innerHTML=hour1+"时";
document.getElementById("3").innerHTML=minute1+"分";
document.getElementById("4").innerHTML=second1+"秒";
}
setInterval(mess,1000);}



html代码:

<!Doctype html>
<html>
<head>
<meta http-equiv="charset" content="utf-8"/>

</head>
<body>
<div class="contioner">
<form>目的日期:
<input id="inyear" name="year" type="text"><span>年</span>
<input id="inmonth" name="month" type="text"><span>月</span>
<input id="inday" name="day" type="text"><span>日</span>
<input id="inhour" name="hour" type="text"><span>时</span>
<input id="inminute" name="minute" type="text"><span>分</span>
<input id="insecond" name="second" type="text"><span>秒</span>
<input type="button" value="确定" onclick="show()">
</form>
<div>还剩:
<span id="1"></span>
<span id="2"></span>
<span id="3"></span>
<span id="4"></span>

</div>
</div>
<script type="text/javascript" src="new.js">
</script>
</body>
</html>