0

I'm trying to get an if statement true in my javascript. I track the current time with a setInterval(function). In this Interval I compare a planned datetime with the current datetime:

first I checked it with a simple if(date1 == date2) but I found on the internet you should use getTime() (found that here) I got this now but it still gives a false when the values are equal:

setInterval(function()
{
    console.log("current time: " + currentTime);
    console.log("end production time: " + endProdTimeMachine2);
    console.log(currentTime.getTime() === endProdTimeMachine2.getTime());
    
    if(currentTime.getTime() === endProdTimeMachine2.getTime())
    {
         console.log("I'm in!!!");
    }
},1000);

When I run this code, my console give this as a result:enter image description here

So what is going wrong? is it something with the interval?

Community
  • 1
  • 1
Bart88
  • 163
  • 1
  • 2
  • 10
  • Did you try verifying what the actual numerical values are that currentTime.getTime() and endProductTimeMachine2.getTime() give? It should give you clear numbers that would make for simpler debugging? – Marcus Feb 17 '16 at 08:59
  • @Marcus ah ye, it's about miliseconds. – Bart88 Feb 17 '16 at 09:05

3 Answers3

2

I assume that milliseconds do not match in your check, because the start of setInterval can be at any millisecond:

if(currentTime.getTime() === endProdTimeMachine2.getTime())

You can change your code, and check only if this timestamp is bigger, and afterwards clear the timeout, to stop further checks:

var interval = setInterval(function() {
    if(currentTime.getTime() >= endProdTimeMachine2.getTime()) {
         console.log("I'm in!!!");
         clearInterval(interval);
    }
}, 1000);
Borys Kupar
  • 1,631
  • 11
  • 24
  • Well it kinda needs to roll through the interval function. So I have to check if the currentTime is between endprodtime +1000 and -1000 or smthing – Bart88 Feb 17 '16 at 09:12
  • Hm, but the interval will run each 1 sec, this will guarantee that it will execute at max time - endprodtime + 999, depending at which millisecond setInterval had started. – Borys Kupar Feb 17 '16 at 09:16
1

Problem:

A 1000ms interval doesn't guarantee that you'll read it in the exact same millisecond, in fact it's quite improbable.

You're running that function each second but reading milliseconds, so you're open to a 999ms displacement.

Solution:

To avoid that, compare it with endProduction in a 999ms range, so you'll know that you're close enough to that exact moment in time.

Example with a range between -499ms and +500ms:

setInterval(function()
{
    console.log("current time: " + currentTime);
    console.log("end production time: " + endProdTimeMachine2);

    var currentTimeMs = currentTime.getTime();
    var endProdTimeMs = endProdTimeMachine2.getTime();
    
    if (currentTimeMs >= (endProdTimeMs - 499) && currentTimeMs <= (endProdTimeMs + 500) )
    {
         console.log("I'm in!!!");
    }
},1000);

Otherwise, if you need it after, change the comparison to:

if (currentTimeMs >= endProdTimeMs && currentTimeMs <= (endProdTimeMs + 999) )
Community
  • 1
  • 1
Ignacio Lago
  • 2,432
  • 1
  • 27
  • 33
0

I can give you an example:

var d1 = "abcd";
var d2 = "abcd";
console.log(d1 == d2); //prints true

d1 = new Date();
d2 = new Date(d1);
console.log(d1 == d2); //prints false

d1 = d2;
console.log(d1 == d2); //prints true

To compare numerical values with getTime():

d1 = new Date();
d2 = new Date(d1);
console.log(d1.getTime() == d2.getTime()); //prints true
PRVS
  • 1,612
  • 4
  • 38
  • 75