I am adapting the following code, that redraws a line at the end of each transition.
function tick() {
// push a new data point onto the back
data.push(random());
// redraw the line, and slide it to the left
path
.attr("d", line)
.attr("transform", null)
.transition()
.duration(500)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)")
.each("end", tick);
// pop the old data point off the front
data.shift();
}
Source of code: https://gist.github.com/mbostock/1642874
However, instead of automatically re-calling tick() at the end of every transition, I would like to check whether a certain boolean (myVar) is true, and only in that case re-call tick().
I have tried to write something like this, but the tick() function has stopped being re-called. Can someone please help me understand what's wrong. Thanks.
function tick() {
// push a new data point onto the back
data.push(random());
myVar = true //for now just hardcoding the value of myVar
// redraw the line, and slide it to the left
path
.attr("d", line)
.attr("transform", null)
.transition()
.duration(500)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)")
.each("end", function() { if (myVar === true) return tick;});
// pop the old data point off the front
data.shift();
}
Note: The new code is adapted from JesperWe's answer here: Invoke a callback at the end of a transition