This tutorial shows you how to build a countdown widget, which counts down to a specific date. It also contains a registration widget where site visitors can register and receive an email notification the day before your event. You will also learn how to install your countdown widget on a site and explore all the key features of Blocks.

To make things easier, we've created a template so that you can get started right away.
The template contains:
Get the template
To build this widget, start with this template and edit it under your Wix account. The template has been left incomplete so that you can learn how to add elements to your widget.
The widget is designed with a grid so that you can easily align and place elements.
in the top bar.
.
.
.Learn more about designing your widget


You can define an API for your widget, so that the site builder who installs the widget can interact with it through code. The API can contain properties, events and exported functions.
This widget uses two properties:
Learn more about widget API properties
First you need to give your widget's elements an ID so that you can easily refer to them in the code.
panel. In this example, the ID is secondsTxt. Note that the other elements have already been given IDs.endDate.
icon that appears.emailId. This is a Text type property with no default value.To make your widget count down, you need to create a function in the code that calculates the remaining time until the end date. To do this, your template comes with some pre-installed utilities that you can find in the Public & Backend
menu.
Blocks uses a new global variable, $widget and the property, $widget.props, that holds all the properties that you have defined for your widget.
Blocks also uses Velo autocomplete, so that you can write code more easily.
Learn more about the widget API
menu.updateTime(), so that your countdown widget will update the count. Your new function should look like this:You also need to set an interval for how often the widget updates the remaining time.
To do this, add the following code in your onReady() function. This updates the widget every second.
Edit time condition Velo code only runs when you click preview or when you open the live site. The Blocks onReady code runs also during editing time, so that you can see it in action while working in the editor. We use the above condition so that your countdown widget doesn't count down during editing time.
You are ready to preview your widget and its API properties.

You can create a custom settings panel so that site builders can change the settings of your widget when they install it on a site. In this example, the custom settings panel allows the site builder to change the end date and define an email ID that determines which email will be sent to users when they register to receive notifications.
Learn more about panels
. Give your text input an ID - endDateInput.emailIdInput.The design of your custom panel is complete. Now you need to implement it by adding code.

You want your custom panel to display the current end date and email ID when it loads. The site builder uses the panel to change these values. You need to add code to the panel so that when these inputs change, it updates your widget.
Learn more about adding code to your custom panels.
To enable panel elements to interact with your widget and perform actions in the editor, you can use the Velo wix-widget module in your code.
To use the Widget API, import wixWidget from the wix-widget module.
Insert the following code before your onReady() function.
You want your widget to update when the values change, so you need to register an onChange event. You also need to add async to your onReady() function as you will be using some asynchronous functions.
Your onReady() function should look like this:
Click
Run or Preview to check your code

Now that your panel is designed and coded, you need to configure your widget so that your panel connects to one of your widget's action bar buttons.
The Configuration tab within the Editor Experience Panel
enables you to control how your widget behaves when a site builder installs and customizes it on a site. You can give your widget and its elements display names, so that it is clear to site builders what your widget does. You can also make changes to the floating action bars that appear in the editors when site builders select elements in your widget.
Learn more about configuration.
.
Action Button Settings. The Main Action Settings panel appears.
The template comes with a second widget called Registration. You can find it under App Interface.
When you click Layers you can see that it has been created as a multi-state box with three states. It has a button labeled Register, which changes to Submit when the site visitor clicks it. It also has a field box where site visitors can add their email address. A thank-you message appears when a site visitor has registered.
This widget also comes with a design preset which can be used for mobile view.
Learn more about design presets
You're now ready to add your registration widget to your first widget, the countdown widget. In Blocks, you can create lots of different widgets and add them to other widgets.
Learn more about working with widgets within widgets
and select Add Widget.registration in Properties & Events
.Another way to add a widget
You can also add an inner widget by clicking the
Add Elements menu. Select MY WIDGETS and drag and drop the widget you want to add into the first widget.


You'll implement the code for user registration in the main widget (Countdown), using functions that are defined in the contactUtils.js and collectionUtils.js files.
The onSubmit() function creates or updates a contact using the wix-crm API via the createContact utility, then stores the subscription data in your app's collection. The function receives the user's email address from the registration form and updates the collection.
onSubmit. It uses the create contact utility from contactUtils.js and the collection name helper from collectionUtils.js.Your code should look like this:
In your onReady(), after your updateTime() function, add the following code:
Backend code is already included The backend code is already implemented in your template in Blocks. It includes an export function, notify(), which uses the pre-installed utilities. It checks the remaining time, triggers the email when ready and updates the collection. You need to invoke this in the editor, as explained below in Step 10.
Now you are ready to release your app for the first time. Your first release will be a major version. Later, when you are working on your app, you can choose a minor or major version. A minor version is updated automatically on all the sites where it appears. You just need to refresh the page. In a major version, the site builder needs to update your app manually. An indication appears next to the name of the app in the Installed Apps panel in the Editor.
Learn more about versions in Blocks
When you click build for the first time, Blocks prompts you to give your app a namespace. This namespace is used to refer to your collection in Velo code in the editors and in the app’s code in Blocks.
Learn more about the app namespace
Now you need to create a collection to store all of the subscriptions. Collections in Blocks are empty placeholders that you design in Blocks, which will be filled with data once the app is installed on a site. Learn more about collections in Blocks
The template has a pre-installed utility called getSubscriptionsCollectionName(), in collectionUtils.js under Public & Backend. It constructs the full name of the collection so that you don't have to add your full app namespace every time you refer to it in the code.
To configure your collection utility:
Click collectionUtils.js under Public & Backend.
Add your app's namespace. Your code should look like this, with your own namespace:
To create your collection:

You want any site visitor to be able to add content to your collection by subscribing.
for your collection from the CMS menu.

You can install your widget on any of the editors. The following example uses the Wix Editor. Before you install your app, you need to release your app a second time. This will be a major release, because you have created a collection.
.
.Your widget is fluid and you can select its elements so that you can customize the widget to your site. You can also open the settings panel and change the default end date.

Users can register for an email notification. You need to create the email that will be sent and get an ID for it so that the widget API can identify which email needs to be sent.
Backend code is included with your app in Blocks, but you need to invoke the notification in the editor.
backend.web.js.Your code should look like this:
Now create an export function invokeNotify in backend.jsw in your site's code section.
Your code should look like this:
Now you need to create a scheduled job. The widget's frontend code only runs when someone is on the page. You need a backend process that runs even when no one is visiting to check who should be notified and send emails. A Scheduled Job is the built‑in way to do that.
in Backend
Add scheduled jobs. Add the following code under jobs.config in your site's code section. In this example, the notify function is invoked at 10 minutes past the hour, every hour.Your code should look like this:
Note: Scheduled jobs run only on the published site. Expect up to 1–2 minutes delay.
Now you are ready to test your app.
Check your triggered emails You can also check in Triggered Emails under Developer Tools in your dashboard to see if the email has been sent.