js 发送短信倒计时、秒杀倒计时实现代码

时间:2021-10-24 13:27:17
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>发送短信倒计时DEMO</title>
<script src="jquery.min.js"></script>
</head>
<body style="padding:20px;">
<span class="djs">clike me </span>
</body> <script>
var status = 1
var seconds = 10
var timeInterval
function timeover(){
$(".djs").html(seconds + "秒")
seconds--
if (seconds < 0) {
status = 1
seconds = 10
clearInterval(timeInterval)
$(".djs").html("clike me")
}
}
$(function(){
$(".djs").click(function(){
if(status == 0){
// 正在处理 - 锁定阶段
return
}
status = 0
timeInterval = setInterval("timeover()", 1000)
})
})
</script>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>秒杀倒计时</title>
<script src="jquery.min.js"></script>
</head>
<body style="padding:20px;">
<span class="showtime"> </span>
</body> <script>
// 当天0点
var zerotime = new Date(new Date().toLocaleDateString()).getTime()
// 当天11点
var time1 = zerotime + 11 * 60 * 60 * 1000 - 1;
// 当天15点
var time2 = zerotime + 15 * 60 * 60 * 1000 - 1;
// 当天20点
var time3 = zerotime + 20 * 60 * 60 * 1000 - 1;
// 第二天11点
var time4 = zerotime + 34 * 60 * 60 * 1000 - 1;
var currenttime
var lefttime = 0 setInterval("timeover()", 1000) function timeover(){
currenttime = new Date().getTime()
if(currenttime < time1) {
lefttime = time1 - currenttime
} else if(currenttime < time2) {
lefttime = time2 - currenttime
} else if(currenttime < time3) {
lefttime = time3 - currenttime
} else if(currenttime < time4) {
lefttime = time4 - currenttime
}
lefttime = lefttime/1000
var result = formatSeconds(lefttime) $(".showtime").html(result[0] + ":" + result[1] + ":" + result[2])
} function formatSeconds(value) {
var secondTime = parseInt(value);// 秒
var minuteTime = 0;// 分
var hourTime = 0;// 小时
if(secondTime > 60) {//如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟大于60,将分钟转换成小时
if(minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60);
//获取小时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60);
}
} var array = [] var hour = parseInt(hourTime) > 9 ? parseInt(hourTime) : "0"+parseInt(hourTime)
var minut = parseInt(minuteTime) > 9 ? parseInt(minuteTime) : "0"+parseInt(minuteTime)
var second = parseInt(secondTime) > 9 ? parseInt(secondTime) : "0"+parseInt(secondTime)
array.push(hour)
array.push(minut)
array.push(second)
return array;
} </script>
</html>

注意: 需要引入JQUERY