我的朋友得到了一个“日出闹钟”,当需要起床时,它会变得更亮,以模拟日出来缓解起床的感觉。
我以为我可以用笔记本电脑做同样的事情,但是我一直在努力让Javascript上班时间。感谢您为使此功能生效而提供的帮助。
我一直试图增加分钟数(wakeupM),无论我想睡多长时间,这都会延迟setTimeout函数,并且仅在我要醒来时才运行。不过,我希望能够在时间输入框中设置时间,并使“日出”功能仅在到达该时间时运行。
预先感谢您的帮助!
var countTime;
function mySleep() {
var now = new Date();
var nowM = now.getMinutes();
var wakeup = new Date();
var wakeupM = wakeup.getMinutes() + 3;
var timeDif = wakeupM - nowM;
countTime = 6000 * timeDif;
document.getElementById("settime").innerHTML = countTime;
}
function mySunrise() {
var colors = ['orange', 'yellow', 'white'];
var active = 0;
setInterval(function() {
document.querySelector('body').style.background = colors[active];
active++;
if (active == colors.length) active = 0;
}, 3000);;
}
body {
background: green;
/* initial color */
transition: background 5s;
/* .5s how long transition should take */
}
<input type="time" id="myTime" value="22:15:00">
<button onclick="setTimeout(mySunrise, countTime); mySleep();">Wake me up</button>
<h1>waking you up at:</h1>
<p id="settime"></p>
最佳答案
我的个人网站上的完成版本-http://piotrirving.com/alarm/sunrise.html
var wakeupH;
var wakeupM;
var wakeupS;
var nowH;
var nowM;
var nowS;
function mySleep() {
var wakeUpTime = document.getElementById("myTime");
var currentVal = wakeUpTime.value;
var res = currentVal.split(":");
wakeupH = res[0]
wakeupM = res[1]
wakeupS = res[2]
var now = new Date();
var nowH = now.getHours();
var nowM = now.getMinutes();
var nowS = now.getSeconds();
var date1 = new Date(2000, 0, 1, nowH, nowM, nowS);
var date2 = new Date(2000, 0, 1, wakeupH, wakeupM, 1);
if (date2 < date1) {
date2.setDate(date2.getDate() + 1);
}
countTime = date2 - date1;
document.getElementById("settime").innerHTML = countTime;
}
function mySunrise() {
var colors = ["#BDB76B", "#F0E68C", "#EEE8AA", "#FFDAB9", "#FFE4B5", "#FFA07A", "#FF7F50", "#FF6347", "#FF4500", "#FF8C00", "#FFA500", "#FFEFD5", "#FAFAD2", "#FFFACD", "#FFFFE0", "#FFFF00", "#FFD700", "white"];
var active = 0;
setInterval(function() {
document.querySelector('body').style.background = colors[active];
active++;
if (active == colors.length) active = 0;
}, 3000);;
}
body {
background: black;
/* initial color */
transition: background 30s;
/* .5s how long transition should take */
color: white;
}
Time: <input type="time" id="myTime" value="22:53:01">
<button onclick="mySleep(); setTimeout(mySunrise, countTime);">Wake me up</button>
<p id="settime"></p>
关于javascript - 日出闹钟-网站,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47250548/