troubleshooting Question

Running Clock Script

Avatar of Bob Schneider
Bob SchneiderFlag for United States of America asked on
JavaScriptAJAXjQuery
72 Comments2 Solutions36 ViewsLast Modified:
I received this script for a running clock utility from this site and it is awesome...except that it starts at 00:00 each time I open the page.  I need it to display the running time from a pre-determined time (lngRaceStartTime).  I can't figure out why that is not happening.  I tried submitting specific values rather than a variable but it always starts at 00:00.  Any help would be much appreciated!

            <h1 class="bg-danger" style="text-align:center;">
                <span id="timer-minutes"></span>:<span id="timer-seconds"></span>
            </h1>
            <script>
                class Timer {
                    constructor(startedTime, min, secs) {
                        this.startedTime = startTime;
                        this.minutes = min;
                        this.seconds = secs;
                        this.intervalTimer = false;
                    }


                    stop() {
                        clearInterval(this.intervalTimer);
                    }


                    start() {
                        this.intervalTimer = setInterval(this.tick.bind(this), 1000)
                    }


                    render(time) {
                        const min = Math.floor(time / 60);
                        const secs = time - (min * 60);
                        this.minutes.innerText = ("00" + min).substr(-2);
                        this.seconds.innerText = ("00" + secs).substr(-2);
                    }


                    tick() {
                        const nowSeconds = Math.floor(Date.now() / 1000);
                        const time = nowSeconds - this.startedTime;
                        
                        this.render(time);
                    }
                }


                // Only do this once, without jQuery - not on every tick
                const min_el = document.getElementById('timer-minutes');
                const secs_el = document.getElementById('timer-seconds');


                // Set your start time here - outside of your class - your class should
                // only operate on what it is given (Dependency Injection)
                const startTime = sessionStorage.getItem(<%=lngRaceStart%>) || Math.floor(Date.now() / 1000);
                timer = new Timer(startTime, min_el, secs_el)
                timer.start();            
            </script>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 72 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 72 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros