<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
// 获取两个时间差, 进行倒计时(不超过20分钟)
var Remainder = null;
function interval(faultDate, completeTime) {
var stime = Date.parse(new Date(faultDate));
var etime = Date.parse(new Date(completeTime));
var usedTime = etime - stime; //两个时间戳相差的毫秒数
var days = Math.floor(usedTime / (24 * 3600 * 1000));
//计算出小时数
var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000));
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / 1000); // 秒数
Remainder = minutes;
return minutes;
}
var createTime = getNowTime('2019-01-16 15:20:00');
var currentTime = getNowTime('2019-01-16 15:30:00');
var nowYim = interval(createTime, currentTime);
console.log(nowYim);
if (nowYim > 1200) {
alert("超出20分钟了")
} else {
countDown(Remainder);
}
function countDown(count) {
if (count == 0) {
console.log("获取验证码 +++++")
return;
}
Remainder = Remainder - 1;
var minute = parseInt(Remainder / 60);
var second = parseInt(Remainder % 60);
console.log('还剩' + minute + '分' + second + '秒');
setTimeout(function () {
count--;
countDown(count);
}, 1000);
}
function getNowTime(res) {
var thisTime = res;
thisTime = thisTime.replace(/-/g, '/');
var time = new Date(thisTime);
time = time.getTime();
return time
}
</script>
</body>
</html>