Counting to three

Small question though Colin. Does that pass the 6-month test? In other words would anyone remember what is happening when coming back to it after 6 months or more?

As you say, there is always more than one way of achieving things. :mage:

Do you want to post the 'technically correct' version Julian, so we can learn from it.

// number of seconds before timestamps expire
// eg for 30 minutes const t = 1800;
const t = 20;

// get the saved array
let ar = flow.get("ar")||[];

// obtain a timestamp
const d = new Date();

// add new timestamp to array (NB: 1 less variable)
ar.push( d.getTime() );

// delete timestamps from array which are older than 't'
const oldest = ts - (1000 * t);
ar = ar.filter(function(v) {
    return v > oldest;
});

// if number of timestamps in array is equal to or
// greater than 3, send a msg & clear the array
if (ar.length >= 3) {
    node.send({ "payload": true });
    ar = [];
    node.status({ "text": ar.length });
} else {
    node.status({ "text": ar.length });
}

// save the array
flow.set("ar", ar);

Since you got me being really picky (unlucky for you I'm having a post-holiday day off work! :nerd_face:), I've also added a little more formatting and white-space, in particular, I like to keep the habit of putting object property names in quotes when the object looks like JSON as this avoids property naming errors and confusion between property names and variable names.

Of course, I'll point out in case anyone thinks I'm getting at someone, I'm not and your code was perfectly fine and working of course. This really is getting into the weeds of JavaScript but personally, I've found habits like these to really help if you do get into more complex code.

1 Like

source

1 Like

It's all about the learning, and if we didn't get help, support & feedback from you and the other senior members, we wouldn't develop or get into bad habits.

When initially writing the code, I did consider using const d = new Date(); but thought that it couldn't be a constant because it will produce a different timestamp every time it was called. BUT having thought this through again after seeing your post, I can see that it's a constant because it's always referencing new Date(); and that never changes. (So I did learn something!)

I've tweaked your code however, because it returns an error "ReferenceError: ts is not defined (line 15, col 16)" The amended code is below;

// number of seconds before timestamps expire
// eg for 30 minutes const t = 1800;
const t = 20;

// get the saved array
let ar = flow.get("ar")||[];

// obtain a timestamp
const ts = new Date().getTime();

// add new timestamp to array (NB: 1 less variable)
ar.push( ts );

// delete timestamps from array which are older than 't'
const oldest = ts - (1000 * t);
ar = ar.filter(function(v) {
    return v > oldest;
});

// if number of timestamps in array is equal to or
// greater than 3, send a msg & clear the array
if (ar.length >= 3) {
    node.send({ "payload": true });
    ar = [];
    node.status({ "text": ar.length });
} else {
    node.status({ "text": ar.length });
}

// save the array
flow.set("ar", ar);

Oops, over-optimised :blush:

1 Like