score:1
first, since you're calling the function it should be repeat()
, not repeat
. also, since i
is defined outside the function repeat
, the callback should be just:
.on("end", function() {
... not:
.on("end", function(i) {
because here i
is undefined
.
but that's not the real problem. the biggest problem here is that it's complicated pausing or delaying javascript loops: that while
loop will run immediately to the end, in few milliseconds, calling repeat
ten times at once.
instead of that, you can just do:
.on("end", function() {
if (i > 9) return;
i++;
repeat();
});
or, to save 1 line:
.on("end", function() {
if (++i > 9) return;
repeat();
});
here is the demo:
var svg = d3.select("body")
.append("svg")
.attr("width", 600)
.attr("height", 100);
function circletransition() {
var timecircle = svg.append("circle")
.attr("fill", "steelblue")
.attr("r", 20);
repeat();
var i = 0
function repeat() {
timecircle
.attr('cx', 40) // position the circle at 40 on the x axis
.attr('cy', 50) // position the circle at 250 on the y axis
.transition() // apply a transition
.duration(2000) // apply it over 2000 milliseconds
.attr('cx', 520) // move the circle to 920 on the x axis
.transition() // apply a transition
.duration(2000) // apply it over 2000 milliseconds
.attr('cx', 40) // return the circle to 40 on the x axis
.on("end", function() {
if (++i > 9) return;
repeat();
});
};
};
circletransition();
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- Repeat the animation only a given number of times
- Why does converting a negative string into a number ( +"-2.333" ) return NaN, only when the string came from d3.format function?
- Simple circle animation is only displayed the first time
- D3 v4 (Tree Diagram) - Dynamically calculate the height of the SVG given dynamic number of nodes
- D3 scale how to put only the minimum and the max number on the scale
- Dynamically resize the d3 tree layout based on number of childnodes
- D3: Is it possible to zoom+pan one axis and only pan the other?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How to speed up the force layout animation in d3.js
- What is the standard way to get the active (running) D3 v3 Transition for a given element?
- maximum number of svg elements for the browser in a map
- D3.js: Pie graph, adding a border only to the outter region
- Can I run a component method only in the client using Angular2 Universal?
- Ticks only at the start and end of an axis
- How to force nvd3 to display the equal number of ticks as that of the values plotted on the graph-nvd3
- Counting the number of continuous blue bars till the hover bar
- Count the number of rows of a CSV file, with d3.js
- After a few clicks the animation freezes, why?
- Display only values from the data set into X axis ticks
- How to find the exact version of D3 given the minified file?
- How to have d3 tickFormat of (day, time) but only show time if day is the same?
- How do I specify the number of axis tick marks in d3?
- Is it possible to have a scrolling div that adjusts to the number of bars in a dc.js rowchart
- How to make the NVD3 discreteBarChart labels on the X axis to adapt to the width or to the number of labels?
- Format number to 2 decimal places only if it has decimal places
- d3 line function works only when I include a dummy parameter in the line function. why?
- Determine the bucket to which a number belongs to
- Make an SVG line appear as an animation "following" the line
- get the parent nodes to a given node in d3 dendogram
- How do I grab only the things I need in D3 v4?
More Query from same tag
- how to integrate d3 with require.js
- C3: Adding y2 axis on the fly
- How to merge objects using javascript
- D3.js - line graph is not displayed
- Chart arcs shrink after refresh
- Sum by category in Piechart (Dc.js & Crossfilter)
- to resize the existing lines in a bar chart
- How can I use d3.layout.stack with this code to draw a line chart using D3.js?
- Passing additional parameter to d3.svg.line()
- d3 javascript change range of the y axis
- Calculate mean json with d3.js
- D3: unable to access data within anonymous function
- D3 Targeting/Control Over Specific Arcs
- D3: how to use the update pattern for on-click-text?
- Add text after last node in D3 Sankey diagram
- D3 toggle stacked to grouped on multiple bar charts
- update d3 pie chart with new data.json
- Unable to dispaly line graph using d3
- selecting/deselecting by clicking in d3
- d3 bar chart implementation in teams viewApp with angular 2
- How can I include add, edit and delete button to d3 tree layout nodes?
- Initialize element and start dragging with just one click
- d3 append and enter issues
- d3.js world topojson v4
- a variable is being set as Nan at one place, but correctly at another location
- animate d3.js line chart path exit
- d3 Axis Bug with Tiny Domain?
- render bar charts in javascript using svg and 2D array from json
- How to animate a path along with its points in d3
- Changing and storing value from radio buttons