为了账号安全,请及时绑定邮箱和手机立即绑定

简单的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("闹钟响了!时间到了!")
}
}

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消