Multiple countdown timers on one page

前端 未结 2 1424
醉话见心
醉话见心 2021-01-03 10:52

Currently working on a project that requires two timers on one page. The timers need to have a start button and both have different timings (i.e. timer1 lasts 10 secs and ti

相关标签:
2条回答
  • 2021-01-03 11:02

    There are few things you're doing that prevent you from expanding the code. If you want a reusable timer, you can't hard set the variables it will use. So first thing is to get rid of the three variables at the top and recreate them WITHIN the scope of the function.

    That way, every time the function is called, a new set of variables is created for its execution.

    The minutes and seconds are probably best passed as parameters and interval should be defined within the scope of function.

    Secondly you are setting the click handler BOTH inline and within the script. Get rid of one of them (preferably get rid of the inline version).

    Then, you have a timer variable as a parameter for the click handler and setInterval but never used. That variable will be set to the click event on the event handler (and again, is not being used) and will be undefined on setInterval. So they really shouldn't be there.

    A performance issue, you may want to know about is that you are doing a DOM lookup for the counters EVERY second. You should get it once per function call at the beginning and cache it.

    At that point, you function would look more or less like this

    function countdown(element, minutes, seconds) {
        // Fetch the display element
        var el = document.getElementById(element);
    
        // Set the timer
        var interval = setInterval(function() {
            if(seconds == 0) {
                if(minutes == 0) {
                    (el.innerHTML = "STOP!");     
    
                    clearInterval(interval);
                    return;
                } else {
                    minutes--;
                    seconds = 60;
                }
            }
    
            if(minutes > 0) {
                var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
            } else {
                var minute_text = '';
            }
    
            var second_text = seconds > 1 ? '' : '';
            el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + '';
            seconds--;
        }, 1000);
    }
    

    And your setup more or less like this

    //Start as many timers as you want
    
    var start1 = document.getElementById('timer1');
    var start2 = document.getElementById('timer2');
    
    start1.onclick = function() {
        countdown('countdown1', 0, 15);
    }
    
    start2.onclick = function() {
        countdown('countdown2', 0, 10);
    }
    

    Of course, you need the extra button and counter

    <div id='countdown1'></div>
    <div id='countdown2'></div>
    <input id="timer1" type="button" value="Start timer 1" />
    <input id="timer2" type="button" value="Start timer 2" />
    

    Working example: http://codepen.io/anon/pen/Jmpcq/?editors=101

    -- Note: for precise timers, setInterval() may not be a good option because the precise interval is not guaranteed and time tracked by it may be delayed after a while. For applications where precise time is critical, there are other methods (e.g. https://sitepoint.com/creating-accurate-timers-in-javascript & https://html5rocks.com/en/tutorials/webperformance/usertimin), thanks @KaiKarver for pointing out in the comments.

    0 讨论(0)
  • 2021-01-03 11:26
    var interval;
    
    var countdown1 = {
        minutes:0,
        seconds: 10
    };
    
    var countdown2 = {
        minutes:0,
        seconds: 2
    }
    
    function countdown(element) {
        alert(element);
        var cd;
        element === 'countdown1' ? cd = countdown1 : cd = countdown2;
        interval = setInterval(function(timer) {
            var el = document.getElementById(element);
            if(cd.seconds == 0) {
                if(cd.minutes == 0) {
                    (el.innerHTML = "STOP!");     
    
                    clearInterval(interval);
                    return;
                } else {
                    cd.minutes--;
                    cd.seconds = 60;
                }
            }
            if(cd.minutes > 0) {
                var minute_text = cd.minutes + (cd.minutes > 1 ? ' minutes' : ' minute');
            } else {
                var minute_text = '';
            }
            var second_text = seconds > 1 ? '' : '';
            el.innerHTML = minute_text + ' ' + cd.seconds + ' ' + second_text + '';
            cd.seconds--;
        }, 1000);
    }
    var start = document.getElementById('start');
    
    
     <div id='countdown'></div>
     <input type="button" onclick="countdown('countdown1');this.disabled = true;" value="Start" />
     <div id="countdown1"></div>
    
     <input type="button" onclick="countdown('countdown2');this.disabled = true;" value="Start" />
     <div id="countdown2"></div>
    

    fiddle: http://jsfiddle.net/dn3hJ/

    0 讨论(0)
提交回复
热议问题