# Checking status change after a given delay

**URL:** <https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806>\
**Category:** General\
**Created:** [4 May 2019 13:12 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806 "2019-05-04T13:12:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedalalousi](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@ahmedalalousi](https://discourse.nodered.org/u/ahmedalalousi)\
**Post date:** [4 May 2019 13:12 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806/1 "2019-05-04T13:12:33Z")

</div>

Folks,

Need a bit of help here.

I'm checking a GPIO change of state. When a change of state happens, I need to wait for a predetermined amount of time, check again and _only take action_ if the status change is the same after that specific period of time has elapsed.

I kind of know that setTimeDelay needs to be used, but couldn't figure out how to use it to achieve my objective - I'm being confused by the callback arrangements and how they work.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 May 2019 14:47 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806/2 "2019-05-04T14:47:41Z")

</div>

If you are doing it in a function node then it is setTimeout that you need to use  
[https://www.w3schools.com/jsref/met\_win\_settimeout.asp](https://www.w3schools.com/jsref/met_win_settimeout.asp)  
However I think node-red-contrib-debounce will do exactly what you want without resorting to a function node.

---

<div class="post-metadata">

**Author:** ![ahmedalalousi](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@ahmedalalousi](https://discourse.nodered.org/u/ahmedalalousi)\
**Post date:** [4 May 2019 15:55 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806/3 "2019-05-04T15:55:50Z")

</div>

Hi Colin,

Thanks for your reply.

It's actually not to debounce a switch - I'm doing this with a GPIO node with a configurable debounce period.

This is to trigger a provisioning routing after a key has been pressed more than a given amount of seconds.

Thanks again ..

Ahmed.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 May 2019 16:19 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806/4 "2019-05-04T16:19:54Z")

</div>

What is the difference? The debounce node will do what you asked.  
If it is the fact that it debounces the Off signal too then you can add a Switch node after the debounce so only On signals get through.

---

<div class="post-metadata">

**Author:** ![tonygruz](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@tonygruz](https://discourse.nodered.org/u/tonygruz)\
**Post date:** [13 April 2022 06:52 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806/5 "2022-04-13T06:52:00Z")

</div>

> [@Colin](#):
>
> If you are doing it in a function node then it is setTimeout that you need to use  
> [https://www.w3schools.com/jsref/met\_win\_settimeout.asp](https://www.w3schools.com/jsref/met_win_settimeout.asp)

setTimeout() and setInterval() functions allow you to execute a piece of JavaScript code/function at a certain point in the future. setInterval repeats the call, setTimeout only runs it once.

[JavaScript setTimeout](http://net-informations.com/q/js/delay.html)(expression, timeout); runs the code/function once after the timeout. It is a time based code execution method that will execute script only one time when the interval is reached, and not repeat again unless you gear it to loop the script by nesting the setTimeout object inside of the function it calls to run. If geared to loop, it will keep firing at the interval unless you call clearTimeout(). If you want something to happen one time after some seconds Then use setTimeout... because it only executes one time when the interval is reached.

```auto
setTimeout(function() {
  console.log('Wait 3 seconds and I appear just once');
}, 3000);

```

setInterval(expression, timeout); runs the code/function repeatedly, with the length of the timeout between each repeat. It is a time interval based code execution method that has the native ability to repeatedly run specified script when the interval is reached. It should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call clearInterval(). If you want to loop code for animations or clocks Then use setInterval.

```auto
setInterval(function() {
  console.log('Every 3 seconds I appear on your console');
}, 3000)

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 April 2022 07:00 UTC](https://discourse.nodered.org/t/checking-status-change-after-a-given-delay/10806/6 "2022-04-13T07:00:27Z")

</div>


