How to Create a Countdown

Материал из Поле цифровой дидактики

A countdown is a timer that reports how much time is left until an event will occur. Projects can also have countdowns too. This tutorial shows how to make one. Within the scope of this tutorial, the timer is counting down to an event at a specific date measured in days. Of course, timers can also be built to be compatible to seconds/minutes with either the built in timer feature or the minute block depending on what the project using it requires.

List

First, a list is needed. A list is a feature which allows the program to stall multiple values at once. Create a list called Dates or anything else that is appropriate.

For each item in the list (except the last), it needs to be the last date of the months before the month of the event. The first item will have the last date of the current month and the last one will have the date of the event. For example, a countdown to the next Scratch Day and it is currently March.

when gf clicked
delete all of [Dates v]
add [31] to [Dates v]
add [30] to [Dates v]
add [9] to [Dates v]

Computing the Countdown

There is now a list but the countdown is not finished. Create a variable called Days left (since the coder is using days) or something else if wanted.

Back to the list, the script below will need to be added to complete the countdown. The first month will be the current month and the last month will be the month of the event. In this case, March (3) is used as the first one and May (5) is used as the last one.

when gf clicked
if <(current [month v]) = (3)> then
replace item (1) of [Dates v] with ((item (1) of [Dates v]) - (current [date v]))
else
replace item (1) of [Dates v] with (0)
if <(current [month v]) = (4)> then
replace item (2) of [Dates v] with ((item (2) of [Dates v]) - (current [date v]))
else
replace item (2) of [Dates v] with (0)
if <(current [month v]) = (5)> then
replace item (3) of [Dates v] with ((item (3) of [Dates v]) - (current [date v]))
end
end
end
set [Days left v] to ((item (1) of [Dates v]) + ((item (2) of [Dates v]) + (item (3) of [Dates v])))