admin管理员组文章数量:1321998
I am trying to make an alarm clock that will play a sound when its done. So, for example, I set the time that the alarm should start at 18:00:00
and the current time is 17:59:00
, so basically the alarm should fire off in 1 min.
I tried doing it in this way.:
var x = '18:00:00';
var t = new Date(x) - new Date();
setTimeout(function(){ alert("Hello"); }, t);
This doesn't work, not sure why. Error is NaN
.
I am trying to make an alarm clock that will play a sound when its done. So, for example, I set the time that the alarm should start at 18:00:00
and the current time is 17:59:00
, so basically the alarm should fire off in 1 min.
I tried doing it in this way.:
var x = '18:00:00';
var t = new Date(x) - new Date();
setTimeout(function(){ alert("Hello"); }, t);
This doesn't work, not sure why. Error is NaN
.
-
1
your var
x
is not even a string which will cause undefined token. – Roljhon Commented Mar 11, 2017 at 16:59 - 2 Do you use your Developer console while programming JS? – Roko C. Buljan Commented Mar 11, 2017 at 17:00
- @Roljhon sorry, I dint paste it properly, changing it – Masnad Nihit Commented Mar 11, 2017 at 17:01
- @RokoC.Buljan I get NAN – Masnad Nihit Commented Mar 11, 2017 at 17:02
3 Answers
Reset to default 6Your problem is the way that x
is defined. The logic behind your code is solid, however you need to use some reasonably smart Datetime manipulation to achieve this. As an example, let's set the alarm for five seconds from the time the page is loaded:
x = new Date();
x.setSeconds(x.getSeconds() + 5);
var t = new Date(x) - new Date();
setTimeout(function(){ alert("Hello"); }, t);
I would suggest you look into Date and figuring out the logic and methods that you need to use to solve your problem.
A rough solution
The logic for an alarm based solely on hours should be like this:
- Create a dummy date from the HH:mm:ss of the alarm time, using the current day, month etc.
- Compare to a
new Date()
to see if it is before or after the current date. - If before the current date, then set
x
to tomorrow and the HH:mm:ss you were given. - Otherwise set to today and the HH:mm:ss.
- Subtract the dates and deal with the alarm.
The following code demonstrates how to implement this:
var x = {
hours: 18,
minutes: 0,
seconds: 0
};
var dtAlarm = new Date();
dtAlarm.setHours(x.hours);
dtAlarm.setMinutes(x.minutes);
dtAlarm.setSeconds(x.seconds);
var dtNow = new Date();
if (dtAlarm - dtNow > 0) {
console.log('Later today, no changes needed!');
}
else {
console.log('Tomorrow, changing date to tomorrow');
dtAlarm.setDate(dtAlarm.getDate() + 1);
}
var diff = dtAlarm - new Date();
setTimeout(function(){ alert("Hello"); }, diff);
Notes:
- HH:mm:ss refers to the hours:minutes:seconds format (see here for more).
- I am also using an object in place of your
x
to make conversion easier. You can easily build the object from a string.
Something like this:
var start = '2017/03/11 12:00:00';
var end = '2017/03/11 12:00:02';
// time in milliseconds:
var time = Math.abs(new Date(start) - new Date(end));
setTimeout(function(){
alert("Hello");
}, time);
I hope this will give you a clear view on what you have now and how you might do it, but there are a lot of ways to do this, I just want to make it more easy for you.
let x = "01:10:30"; //any value you will pass
let date1 = new Date();
//split your x string into hours,minutes and seconds
date1.setHours(x.split(":")[0]); //set hours
date1.setMinutes(x.split(":")[1]); //set minutes
date1.setSeconds(x.split(":")[2]); //set seconds
let t = date1 - new Date();
setTimeout(function(){alert('Hello')},t)
本文标签: javascriptMaking an alarm clock with date timeStack Overflow
版权声明:本文标题:javascript - Making an alarm clock with date time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742116866a2421508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论