继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

简单的JS闹钟。。。。。

田雯和
关注TA
已关注
手记 3
粉丝 2
获赞 85

图片描述

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

打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP