Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active October 10, 2015 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yckart/3728681 to your computer and use it in GitHub Desktop.
Save yckart/3728681 to your computer and use it in GitHub Desktop.
countdownR: Counts an array or given number down
function (
a, // array or number to go through
b, // callback function after counting is finished
c, // the interval (optional)
d, // array-placeholder,
e // isNaN-placeholder
) {
e = +a !==a; // if array isNaN
d = e ? // array isNaN
a.length - 1 : // use the array length
a; // otherwise use the given number
(function t() {
b(
e ? // array isNaN
a[d] : // use the array to count through
[d], // otherwise use the number to count through
!d // check if timer is up
);
d-- && setTimeout(t, c || 1000); // runs the timer-function t() every second
}());
}
function(a,b,c,d,e){e=+a!==a;d=e?a.length-1:a;(function t(){b(e?a[d]:[d],!d);d--&&setTimeout(t,c||1000)}())}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Yannick Albert <http://yckart.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "countDown",
"description": "Counts an array or given number down.",
"keywords": [
"countdown",
"counter",
"counting",
"array",
"number"
]
}
<!DOCTYPE html>
<title>countDown: Counts an array or given number down</title>
<b id="ret"></b>
<script>
var countDown = function(a,b,c,d,e){e=+a!==a;d=e?a.length-1:a;(function t(){b(e?a[d]:[d],!d);d--&&setTimeout(t,c||1000)}())};
countDown(['Done!', 'One', 'Two', 'Three'], function (counter, done) {
document.getElementById('ret').innerHTML = counter;
if (done) alert(true);
});
</script>
@atk
Copy link

atk commented Sep 20, 2012

you can replace isNaN(b) with +b!==b and save 2 bytes. Nice coercion trick to avoid "undefined" by using [d] instead of d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment