<commit_before># Tock #

A javscript timer/countdown clock. 

Based on code by <NAME>:
http://sitepoint.com/creating-accurate-timers-in-javascript/

## Features ##

* Constantly corrects time based on the system clock - won't go out of time.
* Can be used to count up from 0:00 or down from a given time. 
* Can call a callback function every tick (10 milliseconds) and (for countdown clocks) when the clock reaches 0:00.
* Pretty much accurate to about 10 milliseconds (0.01 seconds). 

## How does it work? ##

#### Make some html to show the clock. ####

      <button id="start">Start</button> 
      <button id="stop">Stop</button> 
      <input id="clock" value="10:00">

#### Instantiate a Tock ####

Now in our javascript we make a new instance of Tock and assign it to a variable called *timer*. We'll pass in some options as an object literal while we're at it. *All options are...optional.*

    var timer = new Tock({
        countdown: true,
        interval: 10,
        callback: someCallbackFunction,
        complete: someCompleteFunction
    });

* If *countdown* is true clock will count down from a given time, otherwise it will count up from 0:00. Default: false.

* *interval* is how often, in milliseconds, that the clock will tick. Default: 10.

* *callback* is called every time the clock ticks, once per *interval*. Default: null;

* *complete* is called once the clock reaches 0:00. (only used if countdown = true). Default: null.

#### Control your clock ####

Now you'll need some way of controlling your clock. Let's set up some buttons *(using jQuery)*.

    // Add a start button.
    $('#start').on('click', function() {
	    timer.start($('#clock').val());
	});

Note that we get the time from the clock input and pass it to the start function as the start time.

    // Add a stop button.
    $('#stop').on('click', function() {
	    timer.stop();
	});


#### Add some callback functions ####

Let's replace "someCallbackFunction" above with an anonymous function. Here we use the *lap()* method to get the current clock time in milliseconds, pass that through *msToTime()* to format it nicely and then insert it into the text input we made earlier.

    callback: function () {
        $('#clock').val(timer.msToTime(timer.lap()));
    }

As we are have set *countdown* to *true* we can also replace "someCompleteFunction" with an anonymous function.

    complete: function () {
        alert("Time's up!");
    } 

## Options ##

 * countdown, *bool*
 * interval, *int*
 * callback, *function*
 * complete, *function*

## Methods ##

 * reset()
 * start(time)
 * stop()
 * lap()
 * msToTime(ms)
<commit_msg>added link to demo<commit_after># Tock #

A javscript timer/countdown clock. 

Based on code by <NAME>:
http://sitepoint.com/creating-accurate-timers-in-javascript/

Demo: http://deviouschimp.co.uk/misc/tock

## Features ##

* Constantly corrects time based on the system clock - won't go out of time.
* Can be used to count up from 0:00 or down from a given time. 
* Can call a callback function every tick (10 milliseconds) and (for countdown clocks) when the clock reaches 0:00.
* Pretty much accurate to about 10 milliseconds (0.01 seconds). 

## How does it work? ##

#### Make some html to show the clock. ####

      <button id="start">Start</button> 
      <button id="stop">Stop</button> 
      <input id="clock" value="10:00">

#### Instantiate a Tock ####

Now in our javascript we make a new instance of Tock and assign it to a variable called *timer*. We'll pass in some options as an object literal while we're at it. *All options are...optional.*

    var timer = new Tock({
        countdown: true,
        interval: 10,
        callback: someCallbackFunction,
        complete: someCompleteFunction
    });

* If *countdown* is true clock will count down from a given time, otherwise it will count up from 0:00. Default: false.

* *interval* is how often, in milliseconds, that the clock will tick. Default: 10.

* *callback* is called every time the clock ticks, once per *interval*. Default: null;

* *complete* is called once the clock reaches 0:00. (only used if countdown = true). Default: null.

#### Control your clock ####

Now you'll need some way of controlling your clock. Let's set up some buttons *(using jQuery)*.

    // Add a start button.
    $('#start').on('click', function() {
	    timer.start($('#clock').val());
	});

Note that we get the time from the clock input and pass it to the start function as the start time.

    // Add a stop button.
    $('#stop').on('click', function() {
	    timer.stop();
	});


#### Add some callback functions ####

Let's replace "someCallbackFunction" above with an anonymous function. Here we use the *lap()* method to get the current clock time in milliseconds, pass that through *msToTime()* to format it nicely and then insert it into the text input we made earlier.

    callback: function () {
        $('#clock').val(timer.msToTime(timer.lap()));
    }

As we are have set *countdown* to *true* we can also replace "someCompleteFunction" with an anonymous function.

    complete: function () {
        alert("Time's up!");
    } 

## Options ##

 * countdown, *bool*
 * interval, *int*
 * callback, *function*
 * complete, *function*

## Methods ##

 * reset()
 * start(time)
 * stop()
 * lap()
 * msToTime(ms)

{'fraction_non_alphanumeric': 0.1236418134132634, 'fraction_numerical': 0.010116148370176097, 'mean_word_length': 3.394238683127572, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3155572', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1498, 'n_words': 675}