admin管理员组

文章数量:1323003

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.

Share Improve this question edited Mar 17, 2017 at 9:39 Angelos Chalaris 6,7078 gold badges53 silver badges80 bronze badges asked Mar 11, 2017 at 16:56 Masnad NihitMasnad Nihit 1,9962 gold badges22 silver badges41 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 6

Your 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