# Perspective Script Execution

**URL:** <https://forum.inductiveautomation.com/t/perspective-script-execution/74563>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [June 5, 2023, 1:13pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563 "2023-06-05T13:13:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asjal\_Ahmad1](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Asjal\_Ahmad1](https://forum.inductiveautomation.com/u/Asjal_Ahmad1)\
**Post date:** [June 5, 2023, 1:13pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/1 "2023-06-05T13:13:58Z")

</div>

I am creating a component in Perspective which looks like this.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/2/72bd8de73ff3df627428506b454189107cc3f281.png)

When I click the button, it turns a different color according to this script, which is for the onClick Mouse Event.

```auto
def runAction(self, event):
	if self.view.custom.State == 3:
		self.view.custom.State = 1
	elif self.view.custom.State == 1:
		self.view.custom.State = 3
		then = system.date.now()
		seconds = self.view.params.ackTime
		while True:
			sec_between = system.date.secondsBetween(then, system.date.now())
			if sec_between > seconds:
				break
		self.view.custom.State = 1

```

The button stays that color for self.view.params.ackTime property. The button's color is bound to the self.view.custom.State property and when it changes from 1 to 3, the color of the button changes accordingly.

The problem is that if the button is already in color 3, and I press the button again, the scripts queue up for execution. For example, if the self.view.params.ackTime is set to 3 seconds, and I click the button once, it turns to color 3. But, if I click the button again while it is in color 3, it will extend the total time to 6 seconds. I want it to stop at 3 seconds or just turn the button back to color 1 when it is clicked while the onClick event script is already running. How can I achieve this?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 5, 2023, 1:22pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/2 "2023-06-05T13:22:00Z")

</div>

Timers and while loops in scripts have been discussed many times on the forum and are generally considered to be **a-very-bad-idea**.

If you wish to persist in this approach you could create a boolean session variable to keep track of whether or not a timer is running and then handle that in code. Reset the session variable when the timer is complete.

In general, it would be much safer to have Ignition start the process in question and have the PLC reset the process at timeout. The button status colour would be based off feedback from the PLC and the process will stop even if the browser locks up, is closed, or comms fail.

If you describe what your actual application is (rather than your solution) you may get some better ideas.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 5, 2023, 1:47pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/3 "2023-06-05T13:47:54Z")

</div>

> [@Asjal\_Ahmad1](#):
>
> ```auto
> then = system.date.now()
> seconds = self.view.params.ackTime
> while True:
> sec_between = system.date.secondsBetween(then, system.date.now())
> if sec_between > seconds:
> break
> 
> ```

I think you misspelled `sleep`

---

<div class="post-metadata">

**Author:** ![Asjal\_Ahmad](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Asjal\_Ahmad](https://forum.inductiveautomation.com/u/Asjal_Ahmad)\
**Post date:** [June 5, 2023, 2:01pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/4 "2023-06-05T14:01:09Z")

</div>

I understand that it is a bad idea. I tried using your approach of creating a boolean session variable like this

```auto
def runAction(self, event):
    if self.view.custom.State == 3:
        self.view.custom.State = 1
    elif self.view.custom.State == 1:
        # Check if script is already running
        if self.session.custom.buttonScriptRunning:
            return # Exit the function if script is running
        else:
            self.session.custom.buttonScriptRunning = True # Set the session variable to indicate script is running
            self.view.custom.State = 3
            then = system.date.now()
            seconds = self.view.params.ackTime
            while True:
                sec_between = system.date.secondsBetween(then, system.date.now())
                if sec_between > seconds:
                    break
            # Resume rest of script after values return to 0 and False
            self.view.custom.State = 1
            self.session.custom.buttonScriptRunning = False # Reset the session variable after script execution

```

However, it still doesn't work. The variable changes to True, but for some reason, it does not stop the newer events from running.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 5, 2023, 2:18pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/5 "2023-06-05T14:18:24Z")

</div>

> [@pascal.fragnoud](#):
>
> I think you misspelled `sleep`

Oh, no, that wasn't a misspelled sleep. That was a cpu-hogging denial-of-service attack.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 5, 2023, 2:44pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/6 "2023-06-05T14:44:27Z")

</div>

From your deleted post, change,  
`if self.session.custom.buttonScriptRunning:`  
to  
`if self.session.custom.buttonScriptRunning.value:`

`self.session.custom.buttonScriptRunning` returns  
`[true, Good, Mon Jun 05 15:37:36 BST 2023 (....)]`  
You just want the value.

You still didn't tell us what the funtion of this button is.

---

<div class="post-metadata">

**Author:** ![Asjal\_Ahmad1](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Asjal\_Ahmad1](https://forum.inductiveautomation.com/u/Asjal_Ahmad1)\
**Post date:** [June 5, 2023, 3:18pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/7 "2023-06-05T15:18:11Z")

</div>

basically, that button needs to flash with color 3 when there's a problem, and then temporarily shelve the alarm for the set amount of time by clicking on it and changing its color to color 2 (color 1 is default). The problem occurs when you click it while it is in color 3 state

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 5, 2023, 3:34pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/8 "2023-06-05T15:34:10Z")

</div>

> [@Asjal\_Ahmad1](#):
>
> I want it to stop at 3 seconds or just turn the button back to color 1 when it is clicked while the onClick event script is already running. How can I achieve this?

The generally-accepted method is to disable the button while you don't want additional executions.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 5, 2023, 3:52pm UTC](https://forum.inductiveautomation.com/t/perspective-script-execution/74563/9 "2023-06-05T15:52:39Z")

</div>

Bind the `enabled` property (and maybe it's style class too if you want it to flash) to whatever tells you there's an issue.
