# Stop scripts from running when page is closed

**URL:** <https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [October 10, 2024, 7:10pm UTC](https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103 "2024-10-10T19:10:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Lin](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Andrew\_Lin](https://forum.inductiveautomation.com/u/Andrew_Lin)\
**Post date:** [October 10, 2024, 7:10pm UTC](https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103/1 "2024-10-10T19:10:49Z")

</div>

I have a startup script which I want it to run every 5 min. However, whenever I leave the page (close browser or directly closing the page) and navigating to another page (pervious page is not open anymore) the script is still present in the 'Running Scripts' section. Every time I go back to the page another script is added on the section and now multiple scripts are running.

I am so far using custom property and combining a for loop with time.sleep() to navigate to another page and back. This will close the script after the for loop ends but a constant refresh every 5 min wouldn't be ideal.

Is there a way, I can set it so that my script runs every 5 min as long as the user is on the page and will stops when the user leaves the page.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 10, 2024, 7:33pm UTC](https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103/2 "2024-10-10T19:33:39Z")

</div>

> [@Andrew\_Lin](#):
>
> I have a startup script which I want it to run every 5 min. However, whenever I leave the page (close browser or directly closing the page) and navigating to another page (pervious page is not open anymore) the script is still present in the 'Running Scripts' section. Every time I go back to the page another script is added on the section and now multiple scripts are running.

This is a memory leak, and you are definitely doing something wrong.

> [@Andrew\_Lin](#):
>
> I am so far using custom property and combining a for loop with time.sleep() to navigate to another page and back.

This is bad. Ignition and sleep just really don't work together.

Also, I think you are mixing terminology. You are saying Page, but I think what you really mean is View?

Create a custom property on the view. Use an expression binding with this expression `getMinute(now(1000))`

Then in the propertyChange event script for that property use this script:

```python
if not currentValue.value % 5:
    #run script

```

---

<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:** [October 10, 2024, 7:35pm UTC](https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103/3 "2024-10-10T19:35:08Z")

</div>

Add a custom property to your view.  
Make it an expression binding with this expression: `now(300000)`  
Put your script in the value change script of that property.

---

<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:** [October 10, 2024, 7:38pm UTC](https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103/4 "2024-10-10T19:38:05Z")

</div>

Use a docked view with your page. Add a custom property to the view and use an expression binding like so:

```python
runScript("path.to.script.function", 300000)

```

Place the script in a function in your project library.

---

<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:** [October 11, 2024, 6:59am UTC](https://forum.inductiveautomation.com/t/stop-scripts-from-running-when-page-is-closed/97103/5 "2024-10-11T06:59:17Z")

</div>

Yes, that makes more sense than an on change script... Sometimes I think things only halfway through...
