美文网首页
JS实现倒计时

JS实现倒计时

作者: 游戏藏宝盒 | 来源:发表于2019-08-06 17:29 被阅读0次

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>www.jb51.net JS倒计时</title>

</head>

<body>

<div id="div"></div>

<script type="text/javascript">

setInterval(function() {

var html = clock(true, true, true, false, "", "2019/8/7 00:00")

console.log(html)

}, 1000)

function clock(day, hour, min, sec, startDate, endDate) {

//参数依次为是否返回天数,小时数,分钟数,秒数以及开始时间,结束时间

var nowDate = startDate == "" ? new Date().getTime() : new Date(startDate).getTime() //当前日期时间戳毫秒数

var stopDate = new Date(endDate).getTime() //指定日期时间戳毫秒数

var gapDate = stopDate - nowDate //相差时间戳毫秒数

var haveDay = addZero(Math.floor(gapDate / 1000 / 3600 / 24)) + "天" //相差天数

var haveHour = addZero(Math.floor(gapDate % (1000 * 3600 * 24) / (1000 * 3600))) + "时" //相差小时数

var haveMinutes = addZero(Math.floor(gapDate % (1000 * 3600) / (1000 * 60))) + "分"

var haveSeconds = addZero(Math.floor(gapDate % (1000 * 60) / 1000)) + "秒"

var returnDate = (day ? haveDay : "") + (hour ? haveHour : "") + (min ? haveMinutes : "") + (sec ? haveSeconds : "")

function addZero(num) {

//补零

return num < 10 ? "0" + num : num

}

return returnDate;

}

</script>

</body>

</html>

相关文章

网友评论

      本文标题:JS实现倒计时

      本文链接:https://www.haomeiwen.com/subject/ysvwdctx.html