Search code examples
javascriptflipclock

Getting FlipClock.js to stop after counting up to a number


I am having trouble getting FlipClock.js to stop after it reaches a specific count. I have tried using the "clock.stop();" call but it doesn't work even after many attempts to set a "stop" number. This is the code I have that works for my counter:

<script type="text/javascript">
    var clock;

    $(document).ready(function() {

        // Instantiate a counter
        clock = new FlipClock($('.clock'), {
            clockFace: 'Counter',
            minimumDigits: 4,
        });

        setTimeout(function() {
            setInterval(function() {
            clock.increment();
        }, 0.1);        
        });             
    });
</script>

Any ideas as to how to set my counter to stop at "300"? Any help is greatly appreciated!


Solution

  • EDIT: FlipClock callbacks not being called

    There appears to be a bug in the FlipClock callbacks.

    Here's an alternative solution (you appear to have a lot of extra code, not sure why, this is a trimmed down version):

        var clock,countup;
        clock = new FlipClock($('h1'), {
            clockFace: 'Counter',
            minimumDigits: 4
        });
    
    
        countup = setInterval(function() { 
         if(clock.getTime().time > 300) {
           clock.stop();
           clearInterval(countup);
         }
        },500); 
    

    Check flipclockjs.com for the documentation on callbacks.

    The following is an example:

            var clock,countup;
    
            clock = new FlipClock($('.clock'), {
                clockFace: 'Counter',
                minimumDigits: 4,
                callbacks: {
                 interval: function() {
                   var time = clock.getTime().time;
                   if(time > 300) { clearInterval(countup); }
                 }
                }
            });
    
            setTimeout(function() {
             countup = setInterval(function() { clock.increment(); }, 0.1);        
            });