Skip to content

Instantly share code, notes, and snippets.

@ryanstevens
Forked from 140bytes/LICENSE.txt
Created May 18, 2011 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanstevens/979616 to your computer and use it in GitHub Desktop.
Save ryanstevens/979616 to your computer and use it in GitHub Desktop.
Countdown Timer

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Rules

All entries must exist in an index.js file, whose contents are

  1. a valid Javascript function expression, that
  2. optionally self-executes,
  3. contains no more than 140 bytes, and
  4. does not pollute global scope.

All entries must also be licensed under the MIT license.

For more information

The 140byt.es site hasn't launched yet, but for now follow @140bytes on Twitter.

140byt.es is brought to you by Jed Schmidt. It was inspired by work from Thomas Fuchs and Dustin Diaz.

//play with this @ http://jsfiddle.net/ryanstevens/9t2BG/
function(o, //milliseconds to countdown
i, //interval to check and fire callback
f) //callback function. Number of milliseconds left will be passed through
{
var g='getTime', //cache getTime method name
z=o=o+new Date()[g](), //record the the time to "stop" the countdown
b = function(){
z=o-new Date()[g](); //set remaining milliseconds
if (z+i>=0){ //prevent recursion if z is greater than 0... but allow it to fire one more time
f(z); //fire callback
setTimeout(b, i); //recurse
}
};
b(); //start countdown
}
function(o,i,f){var g='getTime',z=o=o+new Date()[g](),b=function(){z=o-new Date()[g]();if(z+i>=0){f(z);setTimeout(b, i);}};b();}
<!--.
This example will start at 5 seconds and countdown to 0 (ish). It will update the div "about" every 1/2 second.
-->
<div id='test></div>
<script type='text/javascript'>
(function(o, i, f){
var g='getTime',
z=o=o+new Date()[g](),
b = function(){
z=o-new Date()[g]();
if (z+i>=0){
f(z);
setTimeout(b, i);
}
};
b();
})(5000, 500, function(t){
document.getElementById('test').innerHTML = t;
});
</script>
Copyright (c) 2011 YOUR_NAME_HERE, YOUR_URL_HERE
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "CountdownTimer",
"description": "This function will countdown to zero (and go one iteration past zero) by firing a callback on a set interval",
"keywords": [
"140bytes",
"recursion",
"countdown",
"setInterval",
"time"
]
}
@atk
Copy link

atk commented Jul 12, 2011

You can omit .getTime(), because the coercion to Number yields the same value, so instead of (new Date()).getTime(), you can use (+new Date) - so your function boils down to:

function(o,i,f,z,b){o=o+(+new Date);b=function(){z=o-(+new Date);if(z+i>=0)f(z),setTimeout(b,i);};b();}

@reMekElek
Copy link

function(o,i,f,b)(b=function(){if((o-=i)>=0){f(o);setTimeout(b,i);}})();})

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