Lädt...


🔧 Building a Simple Web App to Track Focus Time with Visibility API and Vanilla JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Description

In this blog post, we will go through the process of building a simple web app that tracks the time spent with and without focus using Vanilla JavaScript and the Visibility API. We will also use Tailwind CSS to make it look visually appealing.

The visibilitychange event is a part of the Visibility API that is used to detect changes in the visibility of a web page. When a page becomes hidden or visible, this event is fired. We can use this event to track the time a user spends on a web page with and without focus.

TLDR: We will build a web app to track the time spent with and without focus using the Visibility API and Vanilla JavaScript.

Demo

Over at Codepen.io I have made a short demo: Show how long time an app have been in focus

About Visibilitychange

The Visibility API is a part of the Web API that provides an interface for developers to determine the visibility of a web page. The visibilitychange event is fired when a web page's visibility changes. It is an essential tool for developers to detect if a user is interacting with their web page or not. You can find more information about the Visibility API in the mozilla web API documentation.

The Code

The HTML code of our web app consists of a simple structure that contains two p elements with IDs open-time and blur-time. The open-time element displays the time spent with focus, and the blur-time element displays the time spent without focus.

The JavaScript code uses the setInterval() method to update the time every second. The updateTime() function calculates the time spent with focus and without focus using the Date object and updates the values of the open-time and blur-time elements.

const openTime = document.getElementById("open-time");
const blurTime = document.getElementById("blur-time");

let startTime = new Date();
let blurStartTime = null;
let blurTimeElapsed = 0;

function updateTime() {
  const now = new Date();
  const timeElapsed = now - startTime;
  openTime.textContent = `Time with focus: ${formatTime(timeElapsed)}`;
  if (blurStartTime) {
    blurTimeElapsed = now - blurStartTime;
  }
  blurTime.textContent = `Time out of focus: ${formatTime(blurTimeElapsed)}`;
}

function formatTime(time) {
  const hours = Math.floor(time / (60 * 60 * 1000));
  const minutes = Math.floor((time / (60 * 1000)) % 60);
  const seconds = Math.floor((time / 1000) % 60);
  return `${hours.toString().padStart(2, "0")}:${minutes
    .toString()
    .padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}

setInterval(updateTime, 1000);

document.addEventListener("visibilitychange", function () {
  if (document.hidden) {
    blurStartTime = new Date();
  } else {
    blurTimeElapsed += new Date() - blurStartTime;
    blurStartTime = null;
  }
});


`

Disclaimer

This web app was created as a response to a prompt given by netsi1964 to ChatGPT+, a large language model trained by OpenAI. The purpose of this post is to provide a demonstration of how to use the Visibility API and Vanilla JavaScript to build a simple web app that tracks time spent with and without focus. Image generated using runwayml.com text-to-image tool.

...

🔧 Building a Simple Web App to Track Focus Time with Visibility API and Vanilla JavaScript


📈 91.15 Punkte
🔧 Programmierung

🐧 Vib (Vanilla Image Builder) is Vanilla OS's tool for building container images using YAML recipes.


📈 38.87 Punkte
🐧 Linux Tipps

🔧 Building web components with WebC in vanilla JavaScript


📈 33.11 Punkte
🔧 Programmierung

🔧 🎉 Building Interactive Web Applications with Vanilla JavaScript


📈 33.11 Punkte
🔧 Programmierung

📰 Vanilla OS: More Than Just Vanilla GNOME With Ubuntu


📈 31.95 Punkte
📰 IT Security Nachrichten

🐧 Vanilla OS developers are setting up a testing infrastructure based on Debian sid for Vanilla OS 2.0, Orchid.


📈 31.95 Punkte
🐧 Linux Tipps

🔧 Le saviez-vous ? :focus :focus-within :focus-visible


📈 30.78 Punkte
🔧 Programmierung

🔧 Building a Simple Weather App with HTML, CSS, JavaScript & OpenWeather Api


📈 29.52 Punkte
🔧 Programmierung

🔧 When is the Perfect Time to Switch from Vanilla JavaScript to Vue.js?


📈 28.31 Punkte
🔧 Programmierung

🍏 Cisdem Focus 2.0.0 - Block distracting apps and websites during your focus time.


📈 28.05 Punkte
🍏 iOS / Mac OS

🔧 How to Build a Weather App with HTML,CSS and Vanilla JavaScript


📈 27 Punkte
🔧 Programmierung

🔧 Web Components vs. Vanilla JavaScript: A Niche Comparison in Frontend Technologies


📈 26.19 Punkte
🔧 Programmierung

🔧 Stay Alert ⚠ with JavaScript: Using the Page Visibility API🚀


📈 25.7 Punkte
🔧 Programmierung

📰 Developer Visibility Focus Advances at SmartBear with Senior AI and Observability Hires


📈 25.4 Punkte
📰 IT Security Nachrichten

🔧 How To Build a QR Code Generator App Using Vanilla JavaScript


📈 25.37 Punkte
🔧 Programmierung

🔧 Unlocking Motivation or How I Built a Flashcard App with Vanilla JavaScript to Learn German 🚀📚


📈 25.37 Punkte
🔧 Programmierung

📰 Track This Demonstrates How Advertisers Track You On The Web


📈 24.91 Punkte
📰 IT Security Nachrichten

🔧 Building a simple Full-Stack Restaurant Finder App with React, Redux, Node.js, and Google Places API


📈 24.71 Punkte
🔧 Programmierung

🔧 How the Page Visibility API Improves Web Performance and User Experience


📈 24.68 Punkte
🔧 Programmierung

🔧 Building a Chat App with FastAPI and JavaScript Using JavaScript.


📈 24.37 Punkte
🔧 Programmierung

🔧 How To Create a Before and After Image Slider With Vanilla JavaScript


📈 24.03 Punkte
🔧 Programmierung

🔧 Vanilla JavaScript, Libraries, And The Quest For Stateful DOM Rendering


📈 24.03 Punkte
🔧 Programmierung

🔧 Text Typing Effect in HTML CSS and Vanilla JavaScript


📈 24.03 Punkte
🔧 Programmierung

🔧 How to Upload Files With Vanilla JavaScript and Add a Loading Animation


📈 24.03 Punkte
🔧 Programmierung

🔧 How to track customer sentiment online with Bing News Search API and Text Analytics API


📈 23.68 Punkte
🔧 Programmierung

🔧 How to track customer sentiment online with Bing News Search API and Text Analytics API


📈 23.68 Punkte
🔧 Programmierung

🔧 How to track customer sentiment online with Bing News Search API and Text Analytics API


📈 23.68 Punkte
🔧 Programmierung

🔧 How to track customer sentiment online with Bing News Search API and Text Analytics API


📈 23.68 Punkte
🔧 Programmierung

🔧 How to track customer sentiment online with Bing News Search API and Text Analytics API


📈 23.68 Punkte
🔧 Programmierung

matomo