1

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.

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40

3 Answers3

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.
Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
3

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);
Matt D. Webb
  • 3,216
  • 4
  • 29
  • 51
3

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)
Roljhon
  • 1,279
  • 2
  • 9
  • 22