# Sending msg.payload after Countdown

**URL:** <https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622>\
**Category:** Dashboard\
**Created:** [23 July 2020 17:23 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622 "2020-07-23T17:23:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas703](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andreas703/32/26326_2.png) [@Andreas703](https://discourse.nodered.org/u/Andreas703)\
**Post date:** [23 July 2020 17:23 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/1 "2020-07-23T17:23:04Z")

</div>

Hi, I am not good at Angular JS.  
I am trying to send an msg.payload after an countdown, see the code below.

```auto
 <script>  
    var myTimer;
    function clock() 
    {
       var myTimer = setInterval(myClock, 1000);
        var c = 20;

        function myClock() 
        {
            document.getElementById("demo").innerHTML = --c;

            if (c == 0) 
            {
                
                    (function(scope)
                    {
                        scope.send({payload: "something"});
                    })
                
                (scope); 
              

            }
        }
    }

</script>

<p id="demo" align="center">{{msg}}</p>
<br><br>
<button onclick="clock();">>Start counter</button>

```

```auto
[{"id":"1af6dff0.69f998","type":"debug","z":"eb0da159.829678","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":910,"y":60,"wires":[]},{"id":"c659cd6c.0564c","type":"ui_template","z":"eb0da159.829678","group":"d8ee9c1c.4e4748","name":"","order":3,"width":"12","height":"8","format":" <script> \n var myTimer;\n function clock() \n {\n var myTimer = setInterval(myClock, 1000);\n var c = 20;\n\n function myClock() \n {\n document.getElementById(\"demo\").innerHTML = --c;\n\n if (c == 0) \n {\n \n (function(scope)\n {\n scope.send({payload: \"something\"});\n })\n \n (scope); \n \n\n }\n }\n }\n\n</script>\n\n<p id=\"demo\" align=\"center\">{{msg}}</p>\n<br><br>\n<button onclick=\"clock();\">>Start counter</button>\n","storeOutMessages":true,"fwdInMessages":false,"resendOnRefresh":true,"templateScope":"local","x":120,"y":60,"wires":[["9d6cd887.ff10a8","1af6dff0.69f998"]]},{"id":"d8ee9c1c.4e4748","type":"ui_group","z":"","name":"FRAMATECH Fight Light","tab":"421e8764.5d6f7","order":1,"disp":true,"width":"12","collapse":false},{"id":"421e8764.5d6f7","type":"ui_tab","z":"","name":"FRAMATECH Fight Light","icon":"dashboard","disabled":false,"hidden":false}]

```

But it doesnt work because "19:17:36.597 Uncaught ReferenceError: scope is not defined  
myClock line 20 \> scriptElement:20  
ui:20:1  
"

Could you help me please?

Thx 🙂

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 July 2020 18:28 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/2 "2020-07-23T18:28:04Z")

</div>

Have you tried using a javascript console in your browser to see what might be happening?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 July 2020 18:39 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/3 "2020-07-23T18:39:36Z")

</div>

From the message `scope is not defined` we both know its obvious whats wrong but should we point out the elephant in the room?

This will only work if a browser is open (i.e. this will be client side) - do we think the OP wants this to run when the browser is closed (server/no-red side)? 🤔

---

<div class="post-metadata">

**Author:** ![Andreas703](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andreas703/32/26326_2.png) [@Andreas703](https://discourse.nodered.org/u/Andreas703)\
**Post date:** [23 July 2020 20:52 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/4 "2020-07-23T20:52:19Z")

</div>

Hi Steve, yes it only should run if the Browser is open.

@zenofmud I saw the error in the javascript console 😉  
The error is because of the line with "(scope);"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 July 2020 20:56 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/5 "2020-07-23T20:56:02Z")

</div>

Look at the examples in the built in help on the sidebar. If I remember right, you need to access scope via `this` (however since you will be inside a timer callback, you will likely need to bind this or store `this` in `that` before you setup the timer & access `that.scope`)

---

<div class="post-metadata">

**Author:** ![Andreas703](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andreas703/32/26326_2.png) [@Andreas703](https://discourse.nodered.org/u/Andreas703)\
**Post date:** [27 July 2020 09:35 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/6 "2020-07-27T09:35:47Z")

</div>

Hi @Steve-Mcl,

I dont get it. I dont know where to set this and that. Could you help me pls?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2020 09:44 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/7 "2020-07-27T09:44:30Z")

</div>

Off the top of my head (untested)...

```auto
<script>  
    var that = this; //save a ref to this in that
    var c = 20;
    var myTimer = setInterval(myClock, 1000);

    function myClock() {
        document.getElementById("demo").innerHTML = --c;
        if (c <= 0) {
            that.scope.send({payload: "something"}); // this is not that :) (this is the timers instance, that is original scope)
            clearInterval(myTimer);
        }
    }

</script>

```

---

<div class="post-metadata">

**Author:** ![Andreas703](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andreas703/32/26326_2.png) [@Andreas703](https://discourse.nodered.org/u/Andreas703)\
**Post date:** [27 July 2020 11:33 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/8 "2020-07-27T11:33:22Z")

</div>

> [@Andreas703](#):
>
> ```auto
> function clock() 
> {
> var myTimer = setInterval(myClock, 1000);
> var c = 20;
> 
> ```

thx, but same Issue again :"Uncaught TypeError: that.scope is undefined"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2020 11:38 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/9 "2020-07-27T11:38:16Z")

</div>

You havent posted enough of the `ui_template` for me to see where you have integrated it

---

<div class="post-metadata">

**Author:** ![Andreas703](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andreas703/32/26326_2.png) [@Andreas703](https://discourse.nodered.org/u/Andreas703)\
**Post date:** [27 July 2020 11:40 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/10 "2020-07-27T11:40:55Z")

</div>

oh sry

```auto

 <script>
 
 var that = this; //save a ref to this in that
 var myTimer;
function Clock()
{
    var myTimer = setInterval(myClock, 1000);
    var c = 20;
    function myClock() {
        
        document.getElementById("demo").innerHTML = --c;
        if (c <= 0) {
            that.scope.send({payload: "something"}); // this is not that :) (this is the timers instance, that is original scope)
            clearInterval(myTimer);
        }
    }
}
</script>

<p id="demo" align="center">20</p>
<br><br>
<button onclick="Clock();">>Start counter</button>

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2020 11:51 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/11 "2020-07-27T11:51:08Z")

</div>

Try this...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87c30e59f7c47638405239cd176746c8229a59b9.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33810f2ffdf1d6bc6717cf54009c2ffad66dd23a.png)

```auto
<script>  
(function(scope) {
    
    window.$scope = scope; //save a ref in window/global
    
    //create startClock function
    $scope.startClock = function() {
        $scope.countDown = 20;
        if($scope.myTimer) clearInterval($scope.myTimer);
        $scope.myTimer = setInterval($scope.clockTick, 1000);
    }
    
    //create clockTick function
    $scope.clockTick = function() {
        document.getElementById("demo").innerHTML = --$scope.countDown;
        if ($scope.countDown <= 0) {
            clearInterval($scope.myTimer);
            $scope.myTimer = null;
            $scope.send({payload: "something"}); 
        }
    }    
})(scope);
</script>

<p id="demo" align="center">-</p>
<br><br>
<button onclick="$scope.startClock();">>Start counter</button>

```

---

<div class="post-metadata">

**Author:** ![Andreas703](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andreas703/32/26326_2.png) [@Andreas703](https://discourse.nodered.org/u/Andreas703)\
**Post date:** [27 July 2020 11:53 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/12 "2020-07-27T11:53:51Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> <p id="demo" align="center">-</p>
> <br><br>
> <button onclick="$scope.startClock();">>Start counter</button>
> 
> ```

Thx it works great, and know I try to understand this script xD

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 August 2020 11:53 UTC](https://discourse.nodered.org/t/sending-msg-payload-after-countdown/30622/13 "2020-08-10T11:53:52Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
