Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Catch up on CSS animation after inactive window

I have a countdown timer, in the style of a bar, that slowly decreases in width.

Check the following code:

HTML

<div id="timer"><span></span></div>

CSS

#timer {
    height: 30px;
    display: block;
    overflow: hidden;
    background: grey;
}

#timer span {
    width: 100%;
    height: 100%;
    display: block;
    background: green;
    -webkit-animation-name: decreasewidth;
    animation-name: decreasewidth;
    -webkit-animation-iteration-count: 1;
    animation-iteration-count: 1;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
    -webkit-animation-duration: 20000ms;
    animation-duration: 20000ms;
}

@keyframes decreasewidth {
    to { width: 0% }
}

@-webkit-keyframes decreasewidth {
    to { width: 0% }
}

Sadly, the animation will halt when the page isn't in use, eg. browsing another tab or window. I understand this is ordinary among browsers and helps save processing power.

Problem is, I want my timer to be accurate. That means holding true to its position even when the user flicks to other tabs during. How would I go about achieving this?

At first, my thoughts were along the lines of calculating the amount of ms the user was absent from the page and then updating the timer's span accordingly on return. But this would only happen on the focus event, which relies on user activity.

I also tried a good ol' javascript setInterval technique which simply updated the width of the span every second. Not only did this look terrible - the same problem occurred. Most browsers increase the interval time when inactive.

Interested to hear some ideas on how to handle this.

TL;DR

I need an animated countdown timer bar, that keeps true to its position even when the page is inactive.

EDIT

I know this is noticeable in Safari.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

How about using standard CSS transitions and one line of jQuery?

You can start the loading bar at 100%, with a width transition of 20 seconds. Then, when the page is loaded, add a class that overrides the width and sets it to 0. Because of our transition, it will decrease from 100% to 0% over the course of 20 seconds.

Here's a JSFiddle.

$(document).ready(function() {
    setTimeout(function() {$("#timer span").addClass("load");}, 1);
});
#timer {
    height: 30px;
    display: block;
    overflow: hidden;
    background: grey;
}

#timer span {
    height: 100%;
    display: block;
    background: green;
    transition: 20s width linear;
    width: 100%;
}

#timer span.load{
        width: 0%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="timer"><span class="init"></span></div>

about 4 years ago · Santiago Trujillo Report

0

Looks like the Page Visibility API paired with time absent calculations will do it. Documentation can be found here: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

The events get trigged when the window is visible / invisible, you don't have to click the page in order for it to detect. Exactly what I need.

IE 10+ mind. Although, not a problem for my app :p

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!