cancel
Showing results for 
Search instead for 
Did you mean: 

Add a count down timer to a form

David_Chatterto
Star Collaborator
Star Collaborator

Hi,

 

I have a long questionnaire type Unity form and I have the session timeout to 60 minutes. I'd like to create a count down timer on the form so the user can see how much time they've got left. I've tried a few times but I can't seem to get it working, is this possible?

 

I'm using EP4.

 

Thanks.

1 ACCEPTED ANSWER

Mike_Walkuski
Employee
Employee

Hi @David Chatterton 

 

Here is a working solution that I whipped up really quick that should achieve what you are looking for.

 

The gist of it is,

I set a start time when the form is new and loaded. I then have a "Time Left" Field and a "Hidden Time Left" Field.

I then have a Custom Action that sets the two "Time Left" Fields to each other and then execute a Unity Script that performs the DateTime calculation and updates the Time Left field.

This update from the script then triggers the Custom Action to execute again since the two Time Left fields no longer equal each other and repeats the process.

I use Thread.Sleep in the script to ensure I am not running the script too often.

I have it set to two seconds in this example and am showing seconds instead of minutes to actually demonstrate the behavior easily, but this would work for Minutes too.

 

Example on the form: (These can be hidden as needed)

0c8bc3ea63a94f1ab0d64c918ae68d15

 

 

Custom Action to set "Start Time" and "Hidden Time Left" to kick off the second Custom Action

eaa98a50d721473286ec8255127952a9

 

Custom Action to set the "Time Left" Fields to equal each other and execute the Unity Script.

163fa57692ee454f9f3271c273226a9a

 

Unity Script to perform the calculation and set the "Time Left" field, which in turn will re-trigger the above Custom Action and repeat the process.

e1814f1d8fc5400cb02e057c657a196d

//Wait some time before calculatingThread.Sleep(2000);		//Get the Unity Form Field start timeHyland.Unity.UnityForm.ValueField starttimefield = args.FormInstance.AllFields.ValueFields.Find("starttime");app.Diagnostics.Write(starttimefield.DateTimeValue.ToString());//Add 60 minutes to the start timeDateTime calctimeleft =  starttimefield.DateTimeValue.AddMinutes(60);app.Diagnostics.Write(calctimeleft.ToString());			//Calculate time leftTimeSpan timespan = calctimeleft-DateTime.Now;			//Apply time left to Unity FormHyland.Unity.UnityForm.FormModifier formmodifier = args.FormInstance.CreateUnityFormModifier();app.Diagnostics.Write(timespan.TotalSeconds.ToString());formmodifier.SetFieldValue("timeleft",Convert.ToInt32(timespan.TotalSeconds).ToString());formmodifier.ApplyChanges();

 

Now this is triggering the client to reach out to the Web/AppServer as that is where the Unity Script is executed so I would ensure to test it to make sure it meets you needs and doesn't cause any undesired behavior, but it should be a good starting point for you.

 

I hope this helps! Good Luck!

View answer in original post

6 REPLIES 6

aboucher
Community Manager
Community Manager

Hi @David Chatterton,

 

Can you provide some detail on how you have tried getting  a count down timer on your form to work?  Perhaps a community member can advise you on getting it to work.

 

Meanwhile, this question is still open for all other community members to provide an alternative solution for you.

 

You also have the option to reach out for a Services engagement through your account manager. 

 

Thanks,

~Alan

Hi Alan,

 

I've tried forms.timer as well as timer, using examples on stack exchange and official MS docs, I just can't get them working.

 

Thanks.

Mike_Walkuski
Employee
Employee

Hi @David Chatterton 

 

Here is a working solution that I whipped up really quick that should achieve what you are looking for.

 

The gist of it is,

I set a start time when the form is new and loaded. I then have a "Time Left" Field and a "Hidden Time Left" Field.

I then have a Custom Action that sets the two "Time Left" Fields to each other and then execute a Unity Script that performs the DateTime calculation and updates the Time Left field.

This update from the script then triggers the Custom Action to execute again since the two Time Left fields no longer equal each other and repeats the process.

I use Thread.Sleep in the script to ensure I am not running the script too often.

I have it set to two seconds in this example and am showing seconds instead of minutes to actually demonstrate the behavior easily, but this would work for Minutes too.

 

Example on the form: (These can be hidden as needed)

0c8bc3ea63a94f1ab0d64c918ae68d15

 

 

Custom Action to set "Start Time" and "Hidden Time Left" to kick off the second Custom Action

eaa98a50d721473286ec8255127952a9

 

Custom Action to set the "Time Left" Fields to equal each other and execute the Unity Script.

163fa57692ee454f9f3271c273226a9a

 

Unity Script to perform the calculation and set the "Time Left" field, which in turn will re-trigger the above Custom Action and repeat the process.

e1814f1d8fc5400cb02e057c657a196d

//Wait some time before calculatingThread.Sleep(2000);		//Get the Unity Form Field start timeHyland.Unity.UnityForm.ValueField starttimefield = args.FormInstance.AllFields.ValueFields.Find("starttime");app.Diagnostics.Write(starttimefield.DateTimeValue.ToString());//Add 60 minutes to the start timeDateTime calctimeleft =  starttimefield.DateTimeValue.AddMinutes(60);app.Diagnostics.Write(calctimeleft.ToString());			//Calculate time leftTimeSpan timespan = calctimeleft-DateTime.Now;			//Apply time left to Unity FormHyland.Unity.UnityForm.FormModifier formmodifier = args.FormInstance.CreateUnityFormModifier();app.Diagnostics.Write(timespan.TotalSeconds.ToString());formmodifier.SetFieldValue("timeleft",Convert.ToInt32(timespan.TotalSeconds).ToString());formmodifier.ApplyChanges();

 

Now this is triggering the client to reach out to the Web/AppServer as that is where the Unity Script is executed so I would ensure to test it to make sure it meets you needs and doesn't cause any undesired behavior, but it should be a good starting point for you.

 

I hope this helps! Good Luck!

@Mike Walkuski many thanks, that's awesome!