Search code examples
powerapps

PowerApps - How to disable a button for couple seconds before enabling it?


I have a button named Button_Import, when this button is clicked I will Notify the user that the importing could take some time, then it would call a Data Factory (via Logic Apps) that takes hours to finish.

The issue that this whole clicking process takes only few seconds then the button will be re-enable again.

My goal is to purposely disable the button for around 30 seconds to ensure that there would be no double clicking from the user, hence avoiding a possible duplicate call to the data factory.

How to implement it?


Solution

  • You can use a timer control and a couple of variables to implement this logic. In your button's OnSelect control you can have something similar to the expression below:

    UpdateContext({ startTimer:true, buttonDisabled:true });
    Notify("Imported started, it will take some time...");
    MyDataFactoryProcess.Run(...)
    

    And the button's DisplayMode would use the buttonDisabled variable to determine if it is to be disabled:

    If(!buttonDisabled, DisplayMode.Edit, DisplayMode.Disabled)
    

    In the Timer control you can make it dependent on the startTimer variable, and when it ends it would reset both that and the buttonDisabled variables. Those would be the properties of the timer to update, and their respective values:

    Duration: 30000 // 30 seconds
    Start: startTimer
    OnTimerEnd: UpdateContext({ buttonDisabled:false, startTimer:false })
    

    This way, when the button is pressed, it will disable itself and start the timer; when the timer ends (after the time you specify in its Duration property) it will enable the button and reset the startTimer variable so that when the button is pressed again the timer can start once more.