简单的JS闹钟。。。。。
function startTime() {
var today = new Date(),
y = today.getFullYear(),
month = today.getMonth() + 1,
day = today.getDate(),
h = today.getHours(),
m = today.getMinutes(),
s = today.getSeconds();
y = checkTime(y);
month = checkTime(month);
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
$('#txt').html("现在是北京时间" + ":" + y + "-" + month + "-" + day + " " + h + ":" + m + ":" + s);
t = setTimeout('startTime()', 500);
}
function checkTime(i) {
if(i < 10) {
i = "0" + i;
}
return i;
}
//小时
for(var i = 1; i < 25; i++) {
var str = '';
str += '<option value="' + i + '">' + i + '时</option>';
$('#hour').append(str);
}
//分
for(var t = 0; t < 60; t++) {
var str2 = '';
str2 += '<option value="' + t + '">' + t + '分</option>';
$('#minutes').append(str2);
}
var mytime;
var aH;
var aM;
$(function() {
$(".setting").on("click", function() {
$(".mask,.a").show();
});
$("#canle").on("click", function() {
$(".mask,.a").hide();
});
$("#hour").change(function() {
var nowH = $("#hour").val();
sessionStorage.setItem("now", nowH);
});
$("#minutes").change(function() {
var nowM = $("#minutes").val();
sessionStorage.setItem("nowM", nowM);
});
$("#finsh").on("click", function() {
var d = new Date(),
hour = d.getHours(),
minutes = d.getMinutes(),
nowHour = sessionStorage.getItem("now"),
nowMinutes = sessionStorage.getItem("nowM");
//如果选择时小于现在时并且分数相等
if(nowHour < hour && nowMinutes == minutes) {
aH = 24 - hour + parseInt(nowHour), //小时
aM = 0; //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + aH + "小时" + "0分");
}
//如果选择时小于现在时并且选择分数小于当前分数
if(nowHour < hour && nowMinutes < minutes) {
aH = 23 - hour + parseInt(nowHour); //小时
aM = 59 - (minutes - nowMinutes); //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + aH + "小时" + aM + "分");
}
//如果选择时小于现在时并且选择分数大于当前分数
if(nowHour < hour && nowMinutes > minutes) {
aH = 24 - hour + parseInt(nowHour); //小时
aM = nowMinutes - minutes - 1; //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + aH + "小时" + aM + "分");
}
//如果选择时大于现在时并且分数相等
if(nowHour > hour && nowMinutes == minutes) {
aH = nowHour - hour; //小时
aM = 0; //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + aH + "小时" + "0分");
}
//如果选择时大于现在时并且选择分数小于当前分数
if(nowHour > hour && nowMinutes < minutes) {
aH = nowHour - hour - 1; //小时
aM = 59 - (minutes - nowMinutes); //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + aH + "小时" + aM + "分");
}
//如果选择时大于现在时并且选择分数大于当前分数
if(nowHour > hour && nowMinutes > minutes) {
aH = nowHour - hour; //小时
aM = nowMinutes - minutes - 1; //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + aH + "小时" + aM + "分");
}
//如果选择时等于现在时并且选择分数等于当前分数
if(nowHour == hour && nowMinutes == minutes) {
aH = 23; //小时
aM = 59; //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + "23小时59分");
}
//如果选择时等于现在时并且选择分数大于当前分数
if(nowHour == hour && nowMinutes > minutes) {
aH = 0; //小时
aM = nowMinutes - minutes; //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + "0小时" + aM + "分");
}
//如果选择时等于现在时并且选择分数小于当前分数
if(nowHour == hour && nowMinutes < minutes) {
aH = 23; //小时
aM = 60 - (minutes - nowMinutes); //分钟
sessionStorage.setItem("aH", aH);
sessionStorage.setItem("aM", aM);
mytime = setInterval("timeClock()", 60000);
alert("闹钟倒计时" + "23小时" + aM + "分");
}
$(".mask,.a").hide();
});
});
var timeClock = function() {
var hh = sessionStorage.getItem("aH");
var mm = sessionStorage.getItem("aM");
mm--;
if(mm < 0) {
hh--;
mm = 59;
}
if(hh == 0 && mm == 0) {
hh = 0;
mm = 0;
clearInterval(mytime);
alert("闹钟响了!时间到了!")
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章