Can't stop timeout when get new case

#1

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

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];

    }
#2

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 ?

#3

Also where is timer defined?

#4

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

#5

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.

2 Likes
#6

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