Skip to content

Instantly share code, notes, and snippets.

@mpmckenna8
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpmckenna8/0659f124bc68c1233a62 to your computer and use it in GitHub Desktop.
Save mpmckenna8/0659f124bc68c1233a62 to your computer and use it in GitHub Desktop.
setInterval() to make a function keep repeating

Trying to keep the David Bowie head moving as long as your javascript is running with setInterval().

<!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