少废话,直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script
src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
crossorigin="anonymous"></script>
<script>
function getCountDown(timestamp,num){
setInterval(function(){
var nowTime = new Date();
var endTime = new Date(timestamp * 1000);
var t = endTime.getTime() - nowTime.getTime();
var countDownTime = "00:00:00.0";
if(t > 0) {
var day = Math.floor(t / (24 * 3600 * 1000));
var dayTime = t % (24 * 3600 * 1000);
var hour = Math.floor(t / (3600 * 1000));
var hourTime = dayTime % (3600 * 1000);
var min = Math.floor(hourTime / (60 * 1000));
var minTime = hourTime % (60 * 1000);
var sec = Math.floor(minTime / 1000);
var secTime = minTime % 1000;
var msec = Math.floor(secTime / 100);
if (hour < 10) {
hour = "0" + hour;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
countDownTime = hour + ":" + min + ":" + sec + ":" + msec;
}
$("#countDown" + num).html(countDownTime);
},100);
}
//调用函数:
getCountDown(1573056343,1);
getCountDown(1572969600,2);
</script>
<body>
<p id="countDown1"></p>
<p id="countDown2"></p>
</body>
</html>