cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
User16
Level: Power Up

Button automatically changes color after a period of time

Hi,

 

I have a button that toggles between green and red, depending on what the user selects.

 

If the button is green, I want it to automatically turn back to red after a period of time, for example 24 hours after it was initially toggled to green.

 

Is there functionality to make this happen?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: Button automatically changes color after a period of time

Hi @User16,

I agree with @davidstone's thought almost. You could consider take a try to record the time when you click the button, and store current Button color into your data source.

I assume that your Button should have a initial color, e.g. Green, when you click the button, you could save the click time and current button color into your data source with the following formula:

 

Patch(
  "YourDataSource",
   Defaults("YourDataSource"),
   {
    ...
    ButtonColor: "Green",
    ClickedTime: Now(),
    ...
   }
)

When you click this button again, before submiting the data into your data source, should compre current time with the ClickedTime value of last submitted record of your data source, check if they are in same date. If there are not in a same date and the date difference between these two date values are more than 24 hours (1 day), the Button color would be changed into Red.

 

On your side, you should type the following formula:

Set the OnSelect property of the Button control to following:

If(
   Text(Last("YourDataSource").ClickedTime,"mm/dd/yyyy")<>Text(Now(),"mm/dd/yyyy") && 
   DateDiff(Last("YourDataSource").ClickedTime,Now(),Hours)>=24,
   Patch(
     "YourDataSource",
      Defaults("YourDataSource"),
      {
       ...
       ButtonColor: If(Last("YourDataSource").ButtonColor="Green","Red","Green"),
       ClickedTime: Now(),
       ...
      }
   ),
   Patch(
       "YourDataSource",
        Defaults("YourDataSource"),
        {
          ...
          ButtonColor: Last("YourDataSource").ButtonColor,
          ClickedTime: Now(),
          ...
        }
    )
)

Set the Fill property of the Button control to following formula:

If(
  IsEmpty(Last("YourDataSource"))
  Color.Green,
   If(
       Text(Last("YourDataSource").ClickedTime,"mm/dd/yyyy")<>Text(Now(),"mm/dd/yyyy") && 
   DateDiff(Last("YourDataSource").ClickedTime,Now(),Hours)>=24,
     If(Last("YourDataSource").ButtonColor="Green", Color.Red, Color.Green),
     If(Last("YourDataSource").ButtonColor="Green", Color.Green, Color.Red)
    )
)

More details about the Patch function, please check the following article:

Patch function

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
davidstone
Level 8

Re: Button automatically changes color after a period of time

Is it time specifically that you want to compare against, or could it be days?

 

For example if you were to record in a data source when the button was clicked, you could then compare the date on that item with the current date. If it wasn't today, it's green.

 

If you want it to change in realtime, you could compare the timestamp of the change to 'Now()', except this will only be evaluated once - you'd need a timer to keep polling the current value of 'now' once per second.

User16
Level: Power Up

Re: Button automatically changes color after a period of time

"For example if you were to record in a data source when the button was clicked, you could then compare the date on that item with the current date. If it wasn't today, it's green."

 

Yes, days would work. 

Would you know how to record the button clicking in a data source? Is there a formula for that? 

davidstone
Level 8

Re: Button automatically changes color after a period of time

It depends on your data source. In my case I'm limited to SharePoint, so I would have a data connection to a SharePoint List with a column for 'button name'. When the button is clicked you would write an entry to that list. To write the entry you'd need a hidden form somewhere, and your button would need to dynamically set the values in the form before submitting.

 

That's quite vague because there are quite a few steps to get that solution working, and working efficiently. I'm using a similar solution for a booking system where there are 3 slots per day over 5 working days, so each button click records a booking and sets a timestamp, and a week later those same slots are re-used for the following week, but if the booking is older than 7 days then it's ignored.

Community Support Team
Community Support Team

Re: Button automatically changes color after a period of time

Hi @User16,

I agree with @davidstone's thought almost. You could consider take a try to record the time when you click the button, and store current Button color into your data source.

I assume that your Button should have a initial color, e.g. Green, when you click the button, you could save the click time and current button color into your data source with the following formula:

 

Patch(
  "YourDataSource",
   Defaults("YourDataSource"),
   {
    ...
    ButtonColor: "Green",
    ClickedTime: Now(),
    ...
   }
)

When you click this button again, before submiting the data into your data source, should compre current time with the ClickedTime value of last submitted record of your data source, check if they are in same date. If there are not in a same date and the date difference between these two date values are more than 24 hours (1 day), the Button color would be changed into Red.

 

On your side, you should type the following formula:

Set the OnSelect property of the Button control to following:

If(
   Text(Last("YourDataSource").ClickedTime,"mm/dd/yyyy")<>Text(Now(),"mm/dd/yyyy") && 
   DateDiff(Last("YourDataSource").ClickedTime,Now(),Hours)>=24,
   Patch(
     "YourDataSource",
      Defaults("YourDataSource"),
      {
       ...
       ButtonColor: If(Last("YourDataSource").ButtonColor="Green","Red","Green"),
       ClickedTime: Now(),
       ...
      }
   ),
   Patch(
       "YourDataSource",
        Defaults("YourDataSource"),
        {
          ...
          ButtonColor: Last("YourDataSource").ButtonColor,
          ClickedTime: Now(),
          ...
        }
    )
)

Set the Fill property of the Button control to following formula:

If(
  IsEmpty(Last("YourDataSource"))
  Color.Green,
   If(
       Text(Last("YourDataSource").ClickedTime,"mm/dd/yyyy")<>Text(Now(),"mm/dd/yyyy") && 
   DateDiff(Last("YourDataSource").ClickedTime,Now(),Hours)>=24,
     If(Last("YourDataSource").ButtonColor="Green", Color.Red, Color.Green),
     If(Last("YourDataSource").ButtonColor="Green", Color.Green, Color.Red)
    )
)

More details about the Patch function, please check the following article:

Patch function

 

Best regards,

Kris

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Kudoed Authors (Last 30 Days)
Users online (4,000)