[Accessibility] Create a Human Readable Time Formatter

In this lesson, we are using HTML, accessibility concepts, and ARIA attributes to improve the time scrubber feature of an audio player.

The goal is to make it accessible to users of screen readers so that the time scrubber announces the actual time (e.g. 12 minutes and 15 seconds) rather than just the value (e.g. 735).

We are also creating a new folder called "helpers" in the "src" folder and a new file called "format time.js" within it to hold a function called "format human read time" that takes time as a parameter and returns the human-readable equivalent of that time. Additionally, the author debugs the code and uses console.log to check the variable.

 

Format time helpers

export const formatTime = (time) => {
  // Hours, minutes and seconds
  const hrs = Math.floor(~~(time / 3600)); // eslint-disable-line
  const mins = Math.floor(~~((time % 3600) / 60)); // eslint-disable-line
  const secs = Math.floor(time % 60);

  // Output like "1:01" or "4:03:59" or "123:03:59"
  let ret = "";

  if (hrs > 0) {
    ret += `${hrs}:${mins < 10 ? "0" : ""}`;
  }

  ret += `${mins}:${secs < 10 ? "0" : ""}`;
  ret += `${secs}`;
  return ret;
};

export const formatHumanReadTime = (time) => {
  const formattedTime = formatTime(time);
  const timeArray = formattedTime.split(":").map((time) => parseFloat(time));

  let string = "";
  let minutes;
  let seconds;

  if (timeArray.length > 2) {
    string += `${timeArray[0]} ${timeArray[0] === 1 ? `hour` : `hours`}, `;
    minutes = timeArray[1];
    seconds = timeArray[2];
  } else {
    minutes = timeArray[0];
    seconds = timeArray[1];
  }

  string += `${minutes} ${minutes === 1 ? `minute` : `minutes`}, `;
  string += `${seconds} ${seconds === 1 ? `second` : `seconds`}`;

  return string;
};

 

To make, for example, custom audio player time humun readable, we can use aria-valuetext

<input
  type="range"
  id="time-scrubber"
  value={mediaTime}
  min={0}
  max={duration}
  onChange={onScrubberChange}
  aria-valuetext={formatHumanReadTime(mediaTime)}
/>

 

posted @   Zhentiw  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-01-17 [NGXS] Async Action "Fire and Forgot" && "Fire and Wait"
2019-01-17 [Functional Programming] Draw Items from One JavaScript Array to Another using a Pair ADT
2019-01-17 [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
2018-01-17 [Transducer] Lazyness in Transduer
2018-01-17 [Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types
2018-01-17 [Transducer] Make an Into Helper to Remove Boilerplate and Simplify our Transduce API
2018-01-17 [ML] Daily Portfolio Statistics
点击右上角即可分享
微信分享提示