# Close popup automatically after x seconds in Perspective

**URL:** <https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [May 15, 2024, 4:14pm UTC](https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448 "2024-05-15T16:14:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bfreeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfreeman/32/87436_2.png) [@bfreeman](https://forum.inductiveautomation.com/u/bfreeman)\
**Post date:** [May 15, 2024, 4:14pm UTC](https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448/1 "2024-05-15T16:14:41Z")

</div>

Hi,

I'm trying to close a popup automatically after x seconds in Perspective.

So far I've tried the following script on the view's onStartup event:

```python
def runAction(self):
	def closeLater():
		from time import sleep
		sleep(30)
		system.perspective.closePopup("")
	system.util.invokeAsynchronous(closeLater)

```

But this isn't very clean. If the popup is closed manually the script still runs.

Is there a proper way of achieving this?

Thanks

---

<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:** [May 15, 2024, 4:31pm UTC](https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448/2 "2024-05-15T16:31:55Z")

</div>

When you read your own script and find `sleep()` in there you're not thinking right. Don't!

- Try creating a custom property on the popup view. e.g., `viewOpenedAt`.
- Make the startup script write the time to the property. Something like,  
`self.parent.custom.viewOpenedAt = system.date.now()`
- create another custom property, `viewTimeout`. Create an expression binding on it,  
`(now() - {this.custom.viewOpenedAt}) > 5000`  
This will turn true at 5000 ms.
- Right-click on `viewTimeout` and create a change script. In this add your code to close the popup if the timeout is true.

I haven't tested this. You may need to reset the timeout before you quit so it's ready for the next use.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [May 15, 2024, 4:47pm UTC](https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448/3 "2024-05-15T16:47:19Z")

</div>

What Transistor said, but i'd use an expression binding on viewOpenedAt:

```python
now(0)

```

---

<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:** [May 15, 2024, 9:21pm UTC](https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448/4 "2024-05-15T21:21:04Z")

</div>

> [@josborn](#):
>
> `now(0)`

Of course!  
Thanks.

---

<div class="post-metadata">

**Author:** ![bfreeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfreeman/32/87436_2.png) [@bfreeman](https://forum.inductiveautomation.com/u/bfreeman)\
**Post date:** [May 17, 2024, 1:31pm UTC](https://forum.inductiveautomation.com/t/close-popup-automatically-after-x-seconds-in-perspective/90448/5 "2024-05-17T13:31:23Z")

</div>

> [@Transistor](#):
>
> When you read your own script and find `sleep()` in there you're not thinking right. Don't!

I don't like to use `sleep()` either. The code is inspired from [this answer](https://forum.inductiveautomation.com/t/delaying-a-script-with-popup-displayed-after-in-perspective/48661/3) from @cmallonee on another post.

Thank you @Transistor and @josborn for the proposed alternative.

The finally working setup of the view's custom properties is:

- viewOpenedAt
  - Binding: `now(0)`
  - Persistant: `false`

- viewTimeout
  - Binding: `(now() - {this.custom.viewOpenedAt}) > 30000`
  - Change Script:

```python
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue.value == True:
		system.perspective.closePopup("")

```

A working view example:

```python
{
  "custom": {},
  "params": {},
  "propConfig": {
    "custom.viewOpenedAt": {
      "binding": {
        "config": {
          "expression": "now(0)"
        },
        "type": "expr"
      },
      "persistent": false
    },
    "custom.viewTimeout": {
      "binding": {
        "config": {
          "expression": "(now() - {this.custom.viewOpenedAt}) \u003e 30000"
        },
        "type": "expr"
      },
      "onChange": {
        "enabled": null,
        "script": "\tif currentValue.value \u003d\u003d True:\n\t\tsystem.perspective.closePopup(\"\")"
      },
      "persistent": false
    }
  },
  "props": {},
  "root": {
    "children": [
      {
        "meta": {
          "name": "Label"
        },
        "position": {
          "basis": "32px"
        },
        "props": {
          "text": "This popup will close automatically in 30s",
          "textStyle": {
            "textAlign": "center"
          }
        },
        "type": "ia.display.label"
      },
      {
        "meta": {
          "name": "Label_0"
        },
        "position": {
          "basis": "32px"
        },
        "propConfig": {
          "props.text": {
            "binding": {
              "config": {
                "path": "view.custom.viewOpenedAt"
              },
              "transforms": [
                {
                  "expression": "\"viewOpenedAt: \" + {value}",
                  "type": "expression"
                }
              ],
              "type": "property"
            }
          }
        },
        "props": {
          "textStyle": {
            "textAlign": "center"
          }
        },
        "type": "ia.display.label"
      },
      {
        "meta": {
          "name": "Label_1"
        },
        "position": {
          "basis": "32px"
        },
        "propConfig": {
          "props.text": {
            "binding": {
              "config": {
                "path": "view.custom.viewTimeout"
              },
              "transforms": [
                {
                  "expression": "\"viewTimeout: \" + {value}",
                  "type": "expression"
                }
              ],
              "type": "property"
            }
          }
        },
        "props": {
          "textStyle": {
            "textAlign": "center"
          }
        },
        "type": "ia.display.label"
      }
    ],
    "meta": {
      "name": "root"
    },
    "props": {
      "direction": "column",
      "justify": "center"
    },
    "type": "ia.container.flex"
  }
}
```
