Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Last active October 24, 2016 15:43
Show Gist options
  • Save joshschmelzle/f5ac7499c6cd9cad28a220f216179b27 to your computer and use it in GitHub Desktop.
Save joshschmelzle/f5ac7499c6cd9cad28a220f216179b27 to your computer and use it in GitHub Desktop.
A simple spinner animation with JavaScript and HTML
var a = ["~", "/", "|", "\"];
// - : -
// ~ : ~
var asyncLoop = function(o) {
var i = -1;
var loop = function() {
i++;
if (i == o.length) {
o.callback();
return;
}
o.functionToLoop(loop, i);
}
loop(); //init
}
var start = setInterval(function() {
asyncLoop({
length: 4,
functionToLoop: function(loop, i) {
setTimeout(function() {
document.getElementById("spin").innerHTML = a[i];
loop();
}, 75);
},
callback : function(){
}
})
}, 300);
@joshschmelzle
Copy link
Author

Sample usage:
<span id="spin"></span>
Sample css:
#spin { font-size: 5em; font-family: monospace; }
https://jsfiddle.net/schmelzle/puvyuc4z/

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