# Can't stop timeout when get new case

**URL:** <https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536>\
**Category:** General\
**Created:** [28 March 2019 10:07 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536 "2019-03-28T10:07:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)\
**Post date:** [28 March 2019 10:07 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536/1 "2019-03-28T10:07:37Z")

</div>

I use function and settime out for five case if case one can set timeout ok but when get case 2 that repeat timeout in case one again or get case 3 - 5 that repeat older again

Here my code

```auto
var timeout = context.get("timeout") || 0

function stop_timeout() {
    clearTimeout(timeout)
}

if (lengths == "21") {

        if (timer) {
            clearInterval(timer)
            context.set("timer", 0)
        }

        var imei = newMsg.substr(7, 15);
        var casename = newMsg.substr(22, 1);
        var status_device = newMsg.substr(23, 1);
        var byte_check = newMsg.substr(24, 2);

        if (casename == "1"){

            
            if (timeout) stop_timeout();
            timeout = setTimeout(function(){

                node.send([{
                    "topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
                }], null);
            }, 1000 * 60 * 4 );
            clearTimeout(timeout)
            context.set("timeout", 0)
        }
        
        if (casename == "2"){
            if (timeout) stop_timeout();
            timeout = setTimeout(function(){

                node.send([{
                    "topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
                }], null);
            }, 1000 * 60 * 6 );
            clearTimeout(timeout)
            context.set("timeout", 0)
        } 

        if (casename == "3"){
            if (timeout) stop_timeout();
            timeout = setTimeout(function(){

                node.send([{
                    "topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
                }], null);
            }, 1000 * 60 * 21 );
            clearTimeout(timeout)
            context.set("timeout", 0)
        } 

        if (casename == "4"){
            if (timeout) stop_timeout();
            timeout = setTimeout(function(){

                node.send([{
                    "topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
                }], null);
            }, 1000 * 60 * 6 );
            clearTimeout(timeout)
            context.set("timeout", 0)
        } 

        if (casename == "5"){
            if (timeout) stop_timeout();
            timeout = setTimeout(function(){

                node.send([{
                    "topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
                }], null);
            }, 1000 * 60 * 4 );
            clearTimeout(timeout)
            context.set("timeout", 0)
        } 

        node.send([{
            topic: "UPDATE device_request SET `status`='" + "2" + "',`start_flow`='" + "2" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
        }], null);

        node.send([{
            topic: "INSERT INTO device_ack (`header`, `length`, `cmd`, `imei`, `check1`, `check2`, `casename`, `status_device`, `byte_check`) VALUES ('" + header + "', '" + lengths + "', '" + cmd + "', '" + imei + "', '" + "" + "', '" + "" + "', '" + casename + "', '" + status_device + "', '" + byte_check + "')"
        }], null);

        // msg.topic = "INSERT INTO device_ack (`header`, `length`, `cmd`, `imei`, `check1`, `check2`, `casename`, `status_device`, `byte_check`) VALUES ('" + header + "', '" + lengths + "', '" + cmd + "', '" + imei + "', '" + "" + "', '" + "" + "', '" + casename + "', '" + status_device + "', '" + byte_check + "')";

        // return [msg, null];

    }

```

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [28 March 2019 13:29 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536/2 "2019-03-28T13:29:14Z")

</div>

As far as I can understand your code set a timeout and right after clear it with `clearTimeout(timeout)`

Whatever is the value of the variable `casename` you will not have a timeout set.

Or perhaps I missed something ?

---

<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:** [28 March 2019 14:44 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536/3 "2019-03-28T14:44:53Z")

</div>

> [@junoprima](#):
>
> if (timer) { clearInterval(timer) context.set("timer", 0) }

Also where is `timer` defined?

---

<div class="post-metadata">

**Author:** ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)\
**Post date:** [29 March 2019 01:46 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536/4 "2019-03-29T01:46:41Z")

</div>

OK i change my question i get data from my mqtt and i check length equal 21 and inside if  
case = 1 countdown 3 min  
case = 2 countdown 5 min  
case = 3 countdown 20 min  
case = 4 countdown 5 min  
case = 5 countdown 3 min

but now when equal case 1 countdown 3 min work when case 2 that use 2 countdown when 3 min send and 5 min not do only one

and my flow code this link below because char more than 32000

---

<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:** [29 March 2019 07:14 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536/5 "2019-03-29T07:14:14Z")

</div>

I for one am not going to download and install a massive flow like that. Make a simple flow with an inject, function and debug that shows the problem and post that.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [29 March 2019 11:40 UTC](https://discourse.nodered.org/t/cant-stop-timeout-when-get-new-case/9536/6 "2019-03-29T11:40:41Z")

</div>

> [@junoprima](#):
>
> i change my question

And you should update/correct your javascript code according to suggestions given in such a simplified flow. It is already obvious that you have fundamental errors in your existing code
