Skip to content

Instantly share code, notes, and snippets.

@karthiir
Created March 8, 2017 14:13
Show Gist options
  • Save karthiir/f384d70d0322df4469a517155300548e to your computer and use it in GitHub Desktop.
Save karthiir/f384d70d0322df4469a517155300548e to your computer and use it in GitHub Desktop.
setInterval() to make a function keep repeating
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.floor{
background:violet;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="threeBowie.js"> </script>
</body>
var svg = d3.select('body').append('svg').attr('width', 720).attr('height',270).attr('class','floor');
var daves = [1,2,3,4]
var xpos = 40
// this is what appends some pictures to my svg
svg.selectAll('.bows')
.data(daves)
.enter()
.append('image')
.attr('xlink:href', '/mpmckenna8/raw/f0abe7ae10eef35972bd/icon_21660.svg')
.attr('x', function(d){
return 70 * d
})
.attr('y', 40)
.attr('width',100)
.attr('height',100)
.attr('class','bows');
function dance (){
svg.selectAll('.bows')
.transition()
.delay(500)
.attr('x',function(d){
return Math.random() *720 -29;
})
.attr('y',function(d){
console.log(this.x)
return Math.random() *200 -30;
})
.duration(5000)
};
d3.select('svg')
.on('click',function(){
return dance()}
)
setInterval(dance,6000)
dance()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment