Skip to content

Instantly share code, notes, and snippets.

@erkal
Created April 1, 2014 13:39
Show Gist options
  • Save erkal/9914167 to your computer and use it in GitHub Desktop.
Save erkal/9914167 to your computer and use it in GitHub Desktop.
waitAndEval II

waitAndEval with loop

A simple mechanism for step by step illustrations.

See waitAndEval.

See also playButton.

<!DOCTYPE html>
<meta charset="utf-8">
<title>waitAndEval II</title>
<style>
#messageText{
font-size: 60px;
text-anchor: middle;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
// ----------------------------------------------------------------------
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var messageText = svg.append("text")
.attr("id", "messageText")
.attr("x", width/2)
.attr("y", height/2);
var waitAndEval = newWaitAndEval();
d3.range(100).forEach(function(i) {
waitAndEval(function() { write(i); }, 1000);
})
function write (text) { messageText.attr("class", text).text(text) }
function newWaitAndEval () {
var t = 0;
return function (foo, dur) {
t += dur
setTimeout(foo, t)
};
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment