js時間停止執行,就是這麼任性
阿新 • • 發佈:2018-12-30
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
var t;
function showtime(){
var hdate=new Date('2018-4-28 17:30:00');
var curdate=new Date();
var htime=Date.parse(hdate)/1000;
var ctime=Date.parse(curdate)/1000;
var difftime=htime-ctime;
var mydiv=document.getElementById('result');
var day=difftime/(3600*24);
var intday=parseInt(day);
var hour=(day-intday)*24;
var inthour=parseInt(hour);
var minute=(hour-inthour)*60;
var intm=parseInt(minute);
var second=(minute-intm)*60;
var ints=Math.round(second);
var timestr=difftime;
mydiv.innerHTML='距離五一時間:'+'天'+inthour+'小時'+intm+'分鐘'+ints+'秒';
t=setTimeout('showtime()',1000);
}
function sto(){
showtime();
}
function cto(){
clearTimeout(t);
}
</script>
</head>
<body onload="showtime()">
<div id="result"></div>
<input type="button" value="開始" onclick="sto()" />
<input type="button" value="停止" onclick="cto()" /><br />
</body>
</html>