Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bullet Spinner - an ajax-style loading spinner using padding and setInterval

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!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

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

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

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

function(
e, //the target element where you want to be inserted
i, //the initial position of the bullet
t, //the tick time in milliseconds
a //placeholder, the reference array of padding pixels
){
a="01478741"; //an 8 character string that serves as an array for padding pixels
return setInterval(function(){ //return an interval id so that the interval can be stopped with clearInterval
e.innerHTML="•"; //populate the target element with a bullet (didn't use unicode since the example section doesn't define a charset)
e.style.padding= //update the padding of the target element to move the bullet around
a[i++%8]+ //top padding increments the tick counter and selects one of the numbers from the reference array
"px 0 0 "+ //right and bottom padding are zero
a[(i+1)%8]+ //left padding (like the top padding) uses reference array, only offset by one to make the circular spinning coordinates
"px" //finish the padding string, end result looks something like "1px 0 0 7px"
},t) //set the interval to fire a tick every t milliseconds
}
function(e,i,t,a){a="01478741";return setInterval(function(){e.innerHTML="•";e.style.padding=a[i++%8]+"px 0 0 "+a[(i+1)%8]+"px"},t)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
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": "bulletSpinner",
"description": "Bullet Spinner - an ajax-style loading spinner using padding and setInterval.",
"keywords": [
"ajax",
"animation",
"spinner",
"loading",
"interval"
]
}
<!DOCTYPE html>
<html>
<title>Bullet Spinner</title>
<div style="height:50px">
Simple functionality:
<div id=target></div>
</div>
<div style="height:50px">
Layered functionality:
<div id=target1 style="position:absolute;color:#ddd"></div>
<div id=target2 style="position:absolute;color:#555"></div>
<div id=target3 style="position:absolute;color:#000"></div>
</div>
<script>
var myFunction = function(e,i,t,a){a="01478741";return setInterval(function(){e.innerHTML="&bull;";e.style.padding=a[i++%8]+"px 0 0 "+a[(i+1)%8]+"px"},t)};
//Simple functionality
var loading = myFunction(document.getElementById("target"), 0, 99);
//Layered functionality
var loading1 = myFunction(document.getElementById("target1"), 1, 99);
var loading2 = myFunction(document.getElementById("target2"), 2, 99);
var loading3 = myFunction(document.getElementById("target3"), 3, 99);
//Stop spinning
//clearInterval(loading);
</script>
</html>
tsaniel commented Mar 19, 2014

(i+1)%8 can be replaced with -~i%8

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