All threads / How do I put a live time(seconds) counter on a page?
Ask A Question


You’re not receiving notifications from this thread.

How do I put a live time(seconds) counter on a page?

Arjun Rajkumar asked in Javascript
As in it should go 00:00 .. 00:05.. 00.49 ..15:55 to whatever. 
Wondering how to do this on the front-end.

Should I just use jquery? Or something else? 
Any suggestions?


Hi Arjun,

You could use normal JS with the setInterval method. Something like this: 

function timer(seconds){
    const then = + seconds * 1000;
    countdown = setInterval(() => {
        const secondsLeft = Math.round((then - / 1000);
        if(secondsLeft < 0) {
    }, 1000)

function displayTimeLeft(){
 //update dom

I did something like this as part of a Javascript 30 course by Wes Bos. Recommend checking it out


Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,078+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.