Close popup automatically after x seconds in Perspective

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:

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

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.

5 Likes

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

now(0)
3 Likes

Of course!
Thanks.

2 Likes

I don't like to use sleep() either. The code is inspired from this answer 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:
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue.value == True:
		system.perspective.closePopup("")

A working view example:

{
  "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"
  }
}