Add Timed Feedback to the Page

In Wait for a Pause With Debounce, you built debounce() in the Console, where nothing on a page could change. Sam’s recipe box has two timing problems that debounce() and the timers you know can fix. The status line keeps its last message forever, so an hour after adding Shakshuka, it still says Shakshuka added., and Sam can’t tell old news from new. The Filter field still redraws the list on every keypress.

In this lesson, you’ll make a Saved message that clears itself after two seconds, fade it out by switching a CSS class, and put debounce() on the Filter field. Every change happens in Sam’s recipe box, so the page itself shows you whether it worked. Start with the status line, which Sam reads after every change.

Clear the Saved Message After Two Seconds

After each saved change, Sam wants a short confirmation that then gets out of the way. A new element for it might seem tidier. However, the status line, <p id="status" role="status">, is already a live region.

Screen readers announce new text in a live region politely, when the user isn’t busy with something else. A message that changes quickly can be missed, so not every one gets read out. Still, the live region is how the page offers the same confirmation to screen reader users, so Saved. goes there.

If you don’t have the box’s files yet, the box as this course starts is in the course’s Sample Code download, folder 01-why-async/recipes-start.

Open the box at http://127.0.0.1:3000/recipes/index.html through Live Preview, and open recipes/main.js in your editor. Clearing the message is one job, so it gets one variable for its timer ID and two small helpers. Add them to setUpRecipeBox(), right under let saveFailed = false;:

Language: JavaScript
  let statusTimer;

  function stopStatusClear() {
    clearTimeout(statusTimer);
  }

  function clearStatusSoon() {
    stopStatusClear();
    statusTimer = setTimeout(() => {
      statusLine.textContent = "";
    }, 2000);
  }

The function clearStatusSoon() empties the status line two seconds from now. The function stopStatusClear() calls that off. Both functions are closures over the same statusTimer, as the step tracker in Keep State Private With Closures shared its count.

What happens if Sam saves again while the first message is still waiting to clear? That’s why clearStatusSoon() stops the old timer before it starts a new one. Every new message gets its own two seconds on the status line, and only one clearing timer is ever waiting.

Next, make a successful save say so. In saveRecipes(), add two lines after the whole try...catch, as the function’s last lines:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.