Thread: Countdown timer in a list control

1. Countdown timer in a list control

I have a countdown timer in a list control that tells the user how much time they have left to complete a task. However, it's only showing up on the first record. I'm assuming there needs to be a loop to show through the rest of the list, but I can't figure that part out.

Here's the javascript.

Code:
```function countdown() {

var currentDate = "tue Jan 09 2020 09:06 39";
var currentTime = "19:14:14.9300000";
// Set the date we're counting down to
var countDownDate = new Date(currentDate).getTime() + 43200000;

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);

}```

I have the results showing in a Custom Control on the list with this.

Code:
```function TimeLeft_render as c (ds as p, tmpl as p)
'Specify the Xbasic that returns the HTML for this control.
'This function must return TimeLeft_render, which should
'contain the HTML that will be displayed.
'The 'ds' argument that is passed in contains the current
'values for each of the fields in the current List row.
'For example: ds.data("Firstname")

'-------------------------------------------------------------------
on error goto TimeLeft_xbasicError

'Write Xbasic code here that returns the HTML to be displayed by this control

TimeLeft_render = <<%html%
<p id="demo"></p>
%html%

end
TimeLeft_xbasicError:
TimeLeft_render = "Error in custom control xbasic: " + error_text_get()
end function```
Again, it just shows up on the first record. How would I loop to show them all? I will be making the 'currentdate' and 'enddate' a variable next. Once time has expired then the record won't show up anymore.

Or is there a better way to do this.

No text.

3. Re: Countdown timer in a list control

I've even tried putting all the code in a Virtual Javascript control, but couldn't get the return value right.

4. Re: Countdown timer in a list control

Here's a more simple version of the code.

I've inserted what I think is the loop, but it still only renders in the first record in the list.

Code:
```function countdown() {

var lObj = {dialog.object}.getControl('CHECKOUTCART');
var data = {};

for(i=0; i <lObj._data.length;i++) {
var currentDate = lObj._data[i].ItemDate;
var countDownDate = new Date(currentDate).getTime() + 43200000;
var x = setInterval(function() {

var now = new Date().getTime();

var distance = countDownDate - now;

var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var testing = "test"

document.getElementById("demo").innerHTML = hours + "h "
+ minutes + "m " + seconds + "s ";

if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}

}, 1000);

}
}```
Now, if there's an easier way to get the value to the list, I'm open.

5. Re: Countdown timer in a list control

Anyone have any idea on this?

6. Re: Countdown timer in a list control

Hi David,

Any chance you can create a sample ux with this list control and function with static data. Or a video showing us the properties of the List control and explain the expected output

By looking at you code right way I would say this would only render the first row due to you only updating the id="demo", your Html would only contain one place where this would be true? Each item in the list even though you may of written id="demo" would of automatically appended a number like demo0 demo1 demo2 and so on...

Outside of this technique you can you use javascript to compute the HTML within the List Layout simply use {@[scope].countDownTimer_resolve} within the location in the list controls layout and then select the javascript tab within the List Control (if you dont see this, tick the box in the bottom left hand corner) labelled Optional Tabs

Create a function within here called countDownTimer_resolve with function args of data,context then proceed to write the code there - no need for a loop then or to know an id. As all the information you need is in data.
just put a debugger in there and have a look.

Code:
```debugger;
return '<span>Time should show here</span>'```

7. Re: Countdown timer in a list control

Thank you, Alex. Sooo close. Yay!

I simplified the code so we can get a sample result. I can add back in the rest later pulling the values from the list.

This is what shows the timer on each record, but doesn't update every second. See the line right above the 'Remove' link in the attached image.
Code:
```// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
//var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
return days + "d " + hours + "h " + minutes + "m " + seconds + "s ";

//}, 1000);```
GRAPHICS-0093.png

This is what I want. The setInterval refreshes the timer every 1 second. It returns UNDEFINED as shown in the screen shot.

Code:
```// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
return days + "d " + hours + "h " + minutes + "m " + seconds + "s ";

}, 1000);```
GRAPHICS-0092.png

Apparently, the code doesn't recognize the return value inside of the countdown loop. How do I get the return value from this?

8. Re: Countdown timer in a list control

Hi David,

Yes this is expected, as 1000 milliseconds later when interval starts and all subsequent intervals fire its not being called from the list layout render function to return data.
You don't want to be creating a interval on every list item either.
In your scenario you would only need one interval and use that to update all timers within the List as you had originally had.

The return statement should create and store the required references for a setinterval function to compute the required timer (again bit in the dark on what you do and do not want) as there is so many ways to skin this cat.

Essentially if all the timers are the same this would be the code

Within the List Layout set the html that contains the timer elements for example

HTML Code:
```  <div id="timer">
<div id="days" class="days"></div>
<div id="hours" class="hours"></div>
<div id="minutes" class="minutes"></div>
<div id="seconds" class="seconds"></div>
</div>```

Code:
```// Set the date we're counting down to
{dialog.object}.___countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

function makeTimer() {
// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and the count down date
var distance = {dialog.object}.___countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

jQuery(".days").html(days + "<span>Days</span>");
jQuery(".hours").html(hours + "<span>Hours</span>");
jQuery(".minutes").html(minutes + "<span>Minutes</span>");
jQuery(".seconds").html(seconds + "<span>Seconds</span>");
}

setInterval(function() { makeTimer(); }, 1000);```
Maybe tee up a time and i will go through with you over skype or zoom. Free of Charge my email is alex@acbusinessapps.com.au as long as you post the final solution back on the message board.

9. Re: Countdown timer in a list control

Thanks, Alex.

I guess context would help. I have an ecommerce application and when someone adds something to their cart, the field 'ItemDate' is filled with the current date and time and that item is reserved for that person for 12 hours. When they view the shopping cart, they are displayed with a timer on each item that counts down to 12 hours from the time they added the item to the cart. After 12 hours the item is then removed from their cart and someone else may purchase it.

So, each item counter would be different based on the time they added the item to the cart, counting down to 12 hours from that date/time.

The list control that shows the cart is CHECKOUTCART. Again, the field is ItemDate. And I have a Freeform List Layout.

So, THAT is my goal. Maybe that would make more sense?

10. Re: Countdown timer in a list control

I guess it could be a calculated field then really, right?

11. Re: Countdown timer in a list control

Yes I guess so, but I want it to count down as they look at it.

12. Re: Countdown timer in a list control

I think Charles is pointing in the right direction.

You might begin by recording the time that the item was placed in the cart in the table.

UX components have timers. Maybe set it to 60 seconds, and use the onTimer event to update your display in the list items, display a warning that "You have One Hour left!" or to finally remove the item from the list (and refresh the list).

13. Re: Countdown timer in a list control

I'm finding multiple ways to display the proper countdown on each record, but it's only counting down against the first record. All countdowns are the same, based on that first record. What might i be missing to pull each row's ItemDate?

Thanks, Steve. I will look at the timers. I like the idea of the "You have one hour left!".

14. Re: Countdown timer in a list control

So my thought process is you have a timestamp (a DB field) for when it is placed in the cart to use as a start time, then a second field or text box in the list control that is used to hold the time left, and possibly another ( a text string) to display text based on the calculation. You can use javascript in the calculated field value to display a timer/countdown.
I am only thinking about the setup and not the actual code obviously but I think that's really what would give you what your looking for in the end.

15. Re: Countdown timer in a list control

out of interest
// Display the result in the element with id="demo"
return days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
with 3 list items being displayed, what are the real id's you are using for each item?

16. Re: Countdown timer in a list control

Pete, that's an old comment that doesn't relate to the return value anymore. If you go back to the first post you will see the innerHTML where it's used.

17. Re: Countdown timer in a list control

Hi

Here is a sample UX i built with the technique i would use

this should put us on the same page.

this was built in build 6402

18. Re: Countdown timer in a list control

Within Javascript functions (though could put in timer event without the setInterval)
Code:
```var x = setInterval(function() {

debugger;
var lObj = {dialog.object}.getControl('CHECKOUTCART');

if(lObj){
var data = lObj._data;
for(i=0; i < lObj._data.length;i++) {
//Get Date added to List Control
myDate = data[i].ItemDate
var one_hour = 60 * 60 * 1000; /* ms */

var endTime = new Date(myDate.getTime() + one_hour)
endTime = (Date.parse(endTime) / 1000);

var now = new Date();
now = (Date.parse(now) / 1000);

var timeLeft = endTime - now;
var id = "Timer" + i
var idmsg = "TimerMsg" + i
if(timeLeft > 0){

var days = Math.floor(timeLeft / 86400);
var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));

if (hours < "10") { hours = "0" + hours; }
if (minutes < "10") { minutes = "0" + minutes; }
if (seconds < "10") { seconds = "0" + seconds; }

// Display the result in the element
document.getElementById(id).innerHTML = "Time left: " + hours + "h " + minutes + "m " + seconds + "s ";

if(hours = 0){
document.getElementById(idmsg).innerHTML = "You have one hour left!";
}
} else {
//Note filter the list so the records dont show on next refresh
document.getElementById(id).innerHTML = "Item time has expired";
}
}
}

}, 1000);```
List Control
Data - Static
HTML Code:
```[
{"ItemDate" : "27/01/2020 10:15:00 000 am", "RequiredDate" : "01/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 10:00:00 000 am", "RequiredDate" : "16/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 09:45:00 000 am", "RequiredDate" : "05/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 07:00:00 000 am", "RequiredDate" : "05/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 08:00:50 000 am", "RequiredDate" : "06/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 07:00:00 000 am", "RequiredDate" : "24/08/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 06:30:00 000 am", "RequiredDate" : "08/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 05:00:00 000 am", "RequiredDate" : "09/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 04:00:00 000 am", "RequiredDate" : "12/09/1994 12:00:00 000 am"},
{"ItemDate" : "27/01/2020 03:00:00 000 am", "RequiredDate" : "13/09/1994 12:00:00 000 am"}
]```
Fields
Set ItemDate (Item) Transform Data Type property to: ToDate

List Layout : Freeform
HTML Code:
`{@[scope].countDownTimer_resolve}`
Javascript
Code:
```// Set the date we're counting down to
var renderIndex = data["*renderIndex"]
var idoftimer = "Timer" + renderIndex
var idmsg = "TimerMsg" + renderIndex
//set the inital timer and output result creating an unquie id so we can reference it to update the timer

//Get Date added to List Control
myDate = data.ItemDate
var one_hour = 60 * 60 * 1000; /* ms */
var endTime = new Date(myDate.getTime() + one_hour)
endTime = (Date.parse(endTime) / 1000);
var now = new Date();
now = (Date.parse(now) / 1000);
var timeLeft = endTime - now;

var days = Math.floor(timeLeft / 86400);
var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));

if (hours < "10") { hours = "0" + hours; }
if (minutes < "10") { minutes = "0" + minutes; }
if (seconds < "10") { seconds = "0" + seconds; }

// Display the result in the element with id="Timer[0..x]"
return "<span id='" + idoftimer + "'> Time left: " + hours + "h " + minutes + "m " + seconds + "s </span><span id=" + idmsg + "></span>";```

19. Re: Countdown timer in a list control

That almost works, Alex. I think there was one thing misunderstood. The ItemDate is the date/time that the item was ADDED to the cart, not the time it expires. The countdown should count down 12 hours from the date/time the item was added to the cart. I think you have it counting down to the ItemDate.

And what is the RequiredDate field for? I don't see where that comes into play.

20. Re: Countdown timer in a list control

Got it. changed

var one_hour = 60 * 60 * 1000; /* ms */
to
var one_hour = 43200001; /* ms */

which adds the 12 hours to the ItemDate. And it works as intended. Whoohoo!!! Thank you!!

21. Re: Countdown timer in a list control

Last question on this for now. Now to filter the list based on the expiration time.

Filter where ItemDate is within 12 hours of now. In other words, when the time expires, it filters out so it doesn't show up. Would this be best as an SQL filter or a List filter?

22. Re: Countdown timer in a list control

I would use sql filter as there is no need to have data of expired items being pulled down to client.

23. Re: Countdown timer in a list control

Originally Posted by dfricke10
That almost works, Alex. I think there was one thing misunderstood. The ItemDate is the date/time that the item was ADDED to the cart, not the time it expires. The countdown should count down 12 hours from the date/time the item was added to the cart. I think you have it counting down to the ItemDate.

And what is the RequiredDate field for? I don't see where that comes into play.
RequiredDate was just some left over data when i converted the data source from northwinds db. Please ignore:)

24. Re: Countdown timer in a list control

Originally Posted by dfricke10
Got it. changed

var one_hour = 60 * 60 * 1000; /* ms */
to
var one_hour = 43200001; /* ms */

which adds the 12 hours to the ItemDate. And it works as intended. Whoohoo!!! Thank you!!
Good stuff :) i would update the variable ref name to addTwelveHours so when you or someone else reads the code life is made easier.

25. Re: Countdown timer in a list control

Good idea, Alex. Thanks again.

I can't figure out the SQL filter. I may be overthinking this, but 'ItemDate <= ItemDate + 12 hours' would be the method. Can't get the 'ItemDate + 12 hours' thing right.

26. Re: Countdown timer in a list control

Originally Posted by dfricke10
Good idea, Alex. Thanks again.

I can't figure out the SQL filter. I may be overthinking this, but 'ItemDate <= ItemDate + 12 hours' would be the method. Can't get the 'ItemDate + 12 hours' thing right.

27. Re: Countdown timer in a list control

I think my problem might be the date/time format. I'm finding that time("yyyy-MM-dd 0h:0m:0s",<value>) is the US value, and it appears you're in Australia.

My list throws a conversion error when adding the WHERE clause. I have to save the format time("dd/MM/yyy 0h:0m:0s am",<value>) in a character field. Can't get it to insert into a datetime field.

And, when I use the US datetime format, it enters fine into a datetime field, but it's 1 hour off. It starts at 11 hours instead of 12 on the countdown.

Make sense?

28. Re: Countdown timer in a list control

Originally Posted by alexc
This seems to work.

now() <= AddHoursToDateTime( convert(datetime, OrderItems.ItemDate, 103) , 12)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•